|
楼主 |
发表于 2016-5-20 16:44:13
|
显示全部楼层
#include <c8051F040.h>
#define UCHAR unsigned char
#define UINT unsigned int
#define SYSCLK 24500000
#define BAUDRATE 9600
UCHAR usart0_txd_buf[8] = {0x0e,0xaa,0x00,0x00,0x00,0x00,0x00,0xb8};
UCHAR usart0_rxd_buf[8];
UCHAR usart0_txd[8];
UCHAR flag_usart0_rx;
UCHAR send_conts;
UCHAR Power_Chenl[6];
UCHAR Data_Chenl[6];
UINT FOC_CONTS;
UCHAR FOC_FLAG;
static UCHAR rx_conts;
static UINT FOC_OUT;
static UCHAR sen_flg;
void PORT_INIT(void);
void OS_INIT(void);
void USART_INIT(void);
void TIME_INIT(void);
void send_byte_usart0(UCHAR usart0_dat);
void timer0_init(void);
void port_init();
void os_init();
void uart_init();
void time_init();
void send_byte_usart0(UCHAR usart0_dat)
{
SFRPAGE = UART0_PAGE;
SBUF0= usart0_dat;
while(TI0 == 0);
TI0 = 0;
}
void main()
{
WDTCN = 0xDE; //关闭看门狗 地址是OxFF
WDTCN = 0xAD; //防止意外产生的禁止操作
port_init(); //端口初始化
os_init(); //晶振初始化
uart_init();
time_init();
SFRPAGE = CONFIG_PAGE; //0x0F
IE |= 0x90; //开中断,开总中断,开串行中断
while(1)
{
if(flag_usart0_rx == 0)
{
send_byte_usart0(P4);
send_byte_usart0(P5);
send_byte_usart0(P6&0X07);
}
}
}
void uart0_ISR() interrupt 4
{
if(((rx_conts != 0) || (SBUF0 != 0))&&(flag_usart0_rx == 0))
{
usart0_rxd_buf[rx_conts] = SBUF0;
rx_conts ++;
if(rx_conts >= 8)
{
rx_conts = 0;
if(usart0_rxd_buf[7] == ((usart0_rxd_buf[0] + usart0_rxd_buf[1] + usart0_rxd_buf[2])&0x00ff))
{
flag_usart0_rx = 1;
send_byte_usart0(P4);
send_byte_usart0(P5);
send_byte_usart0(P6&0X07);
flag_usart0_rx = 0;
}
}
}
SFRPAGE=0x00;
if(SCON0&0x01)
SCON0&=0xfe;
if(SCON0&0x02)
SCON0&=0xfd;
}
void port_init()
{
SFRPAGE = CONFIG_PAGE;
P0MDOUT = 0xfc;
P0 = 0xff;
P1MDIN = 0xff;
P1MDOUT = 0x00;
P1 = 0xff;
P2MDIN = 0xff;
P2MDOUT = 0x00;
P2 = 0xff;
P3MDIN |= 0x08;
P3MDOUT = 0xFF;
P3 = 0xff;
P4MDOUT = 0x00;
P4 = 0xff;
P5MDOUT = 0x00;
P5 = 0xff;
P6MDOUT = 0x00;
P6 = 0xff;
P7MDOUT = 0x00;
P7 = 0xff;
XBR0 = 0x04;
XBR1 = 0x00;
XBR3 = 0x00;
XBR2 = 0x40; //使能交叉开关
}
void os_init()
{
SFRPAGE = CONFIG_PAGE; //me Ox84=0x0F代表读或修改的时候所使用的SFR页
OSCXCN = 0x00;
CLKSEL = 0x00;
OSCICN = 0x83;
}
void uart_init()
{
SFRPAGE = UART0_PAGE; //0x01,
SCON0 = 0x50; //允许uart1,同51 SCON用法,8位UART,开始接收
}
void time_init()
{
SFRPAGE = TIMER01_PAGE; //0x00
TCON = 0X40; //启动TIMER1
TMOD = 0x20; //TIMER1 MODE2
CKCON |= 0X10; //时钟控制寄存器0x8E,[NET]//ckcon^01=0x01,sysclk*1/4; ckcon^01=0x00,sysclk*1/12;
PCON |= 0x80;
TH1 = -(SYSCLK/BAUDRATE/16); // ckcon^01=0x02,sysclk*1/48; //ckcon^01=0x03,extra clk *1/8;ckcon=0x08,sysclk,(这个未证实);
TL1 = -(SYSCLK/BAUDRATE/16); //图,SMOD0=0,方式1波特率为:1/32*T1CLK/(256-TH1)
TR1=1; //TCON^6 timer1开关
TI1 = 1;
}
|
|