/**************************************************/
/*** File Name: 256.c ***/
/*** Product Name: SRC-256 ***/
/*** Description: 6 channel DMX controller ***/
/*** MCU: At89c2051(or At89c1051u) Xtal: 8MHz ***/
/*** Author: Hawk Wang, Date: 08/12 ***/
/*** Version: Rev 1.0 ***/
/**************************************************/
/*** IO assign map ***/
/*** P1 ---DAC data out ***/
/*** P3_0 ---NC ***/
/*** P3_1 ---Txd ***/
/*** P3_2 ---comparator out ***/
/*** P3_3 ---4051 a chanel ***/
/*** P3_4 ---4051 b chanel ***/
/*** P3_5 ---4051 c chanel ***/
/*** P3_7 ---573LE(not use) ***/
#pragma ROM(SMALL) OPTIMIZE (5,SIZE)
#include "AT892051.H"
//define crystal frequence
#define XTAL 8 //XTAL: 8MHz
//variable declare
unsigned char AdcDataList[7]; //0:master,1:channel1,2:channel2,etc.
//function declare
void Init(void);
unsigned char GetAdcValue(unsigned char ch_n); //ch_n range: 0~6(channel7 not use)
void DmxOut(void);
//main function
void main(void)
{
unsigned char adc_channel;
Init();
while(1) //runtime of while loop: 37ms(xtal=8mhz)
{
for(adc_channel=0;adc_channel<=6;adc_channel++)
{
AdcDataList[adc_channel] = GetAdcValue(adc_channel);
}
DmxOut();
}
}
/*** Initialize IO,UART ***/
void Init(void)
{
IE = 0x00; //disable all interrupt
P1 = 0xff;
P3 = 0xff;
TCON = 0x00;
#if XTAL == 8
PCON = 0x80; //SMOD=1
#else
PCON = 0x00; //SMOD=0
#endif
SCON = 0x88; //UART mode2,TB8=1,disable receive
}
/*** get adc value ***/
/*** ch_n=0: master ***/
/*** ch_n=1: ch1 ***/
/*** ........... ***/
/*** ch_n=6: ch6 ***/
unsigned char GetAdcValue(unsigned char ch_n)
{
unsigned char value;
P3 = (P3&0xc3)|(ch_n<<3)|0x04; //select ADC channel
P1 = 0xff;
for(value=0xff;(P3_2==1)&&(value>0); )
{
P1 = --value;
}
return (value);
}
void DmxOut(void)
{
unsigned char dimmer_number;
unsigned char delay_time;
TXD = 0; //sent reset,mark after reset
delay_time = 20; //delay (20*6+7)*1.5us>88us
while(delay_time--);//(delay_time*6+7)*12/XTAL
TXD = 1;
delay_time =2; //delay>8us
while(delay_time--);
SBUF = 0x00; //sent start code
while(!TI);
TI = 0;
for(dimmer_number=1;dimmer_number<=6;dimmer_number++)
{
SBUF = (unsigned char) (((unsigned int)(AdcDataList[dimmer_number]+1))*AdcDataList[0]>>8); //runtime=43*(12/XTAL)
while(!TI);
TI = 0;
}
}
相关文章