- 时间:2020.02.19
- 芯片:STC12C5A60S2
- 晶振:11.0592MHz 波特率4800bps
- 引脚定义:串行口1:发送----TxD/P3.1; 接收---.000-RxD/P3
- 串行口2: 发送----TxD2/P1.3;接收----RxD2/P1.2
- 功能描述:STC12双串口通信(中断方式)
- ***********************************************************************/
- #include<STC12C5A60S2.h>
- //#include<reg51.h>
- #include <intrins.h>
- #include<stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define FOSC 11059200L //System frequency
- #define BAUD 4800 //UART baudrate
- #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
- unsigned char Tx_Buffer[8];
- unsigned char RX_Buffer[50];
- unsigned char read_enable,receive_finished,reveive_number,ms,number;
- unsigned long Voltage_data,Voltage,Voltage1;
- unsigned long Vh6,Vl6,Vh5,Vl5,Vh4,Vl4,Vh3,Vl3;
- union crcdata //CRC机构体
- {
- unsigned int word16;
- unsigned char byte[2];
- }crcnow;
- void power_out_mode();
- void Delay_ms(unsigned int n);
-
- sbit led = P2^0;
- sbit WDT_WDI= P3^2;
- /****************串行口初始化函数****************/
- void InitUART(void)
- {
- SCON = 0x50; //8-bit variable UART
- TMOD=0x21; //定时1工作方式于串口,定时器0工作方式1
- TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule定时器1初值
- TR1=1; //启动定时器1
- REN=1; //允许接收中断
- ES=1; //允许串行口中断
- TH0=(65535-50000)/256; //以下设置为时钟,装初值
- TL0=(65535-50000)%256;
- ET0=1; //开定时器0中断
- TR0=1; //启动定时器0
- EA =1; //开总中断
- }
- /****************串行口连续发送char型数组****************/
- void Uart1Sends(unsigned char DAT)
- {
- //ES = 0;
- TI=0;
- SBUF=DAT ;
- while(TI==0);
- TI=0;
- //ES = 1;
- }
- void Send_data(unsigned char lenth )
- {
- unsigned char i = 0 ;
- for(i=0; i<lenth; i++)
- {
- Uart1Sends(Tx_Buffer[i]) ;
- }
- }
- /************CRC***********/
- unsigned int calccrc(unsigned char crcbuf,unsigned int crc)
- {
- unsigned char i;
- unsigned char chk;
- crc=crc ^ crcbuf;
- for(i=0;i<8;i++)
- {
- chk=( unsigned char)(crc&1);
- crc=crc>>1;
- crc=crc&0x7fff;
- if (chk==1)
- crc=crc^0xa001;
- crc=crc&0xffff;
- }
- return crc;
- }
- unsigned int chkcrc(unsigned char *buf,unsigned char len)
- {
- unsigned char hi,lo;
- unsigned int i;
- unsigned int crc;
- crc=0xFFFF;
- for(i=0;i<len;i++)
- {
- crc=calccrc(*buf,crc);
- buf++;
- }
- hi=( unsigned char)(crc%256);
- lo=( unsigned char)(crc/256);
- crc=(((unsigned int)(hi))<<8)|lo;
- return crc;
- }
- /************发送命令***********/
- void read_data(void)
- {
- read_enable=0;
- Tx_Buffer[0]=0x01; //模块的ID 号,默认0x01
- Tx_Buffer[1]=0x03;
- Tx_Buffer[2]=0x00;
- Tx_Buffer[3]=0x48;
- Tx_Buffer[4]=0x00;
- Tx_Buffer[5]=0x08;
- crcnow.word16=chkcrc(Tx_Buffer,6);
- Tx_Buffer[6]=crcnow.byte[1]; //CRC校验低字节在前
- Tx_Buffer[7]=crcnow.byte[0];
- Send_data(8); //发送数据
- }
- /*******端口初始化********/
- void IO_Init()
- {
- led=1;
-
- }
- void main()
- {
- InitUART();
- IO_Init();
- Delay_ms(10);
- while(1)
- {
- WDT_WDI=~WDT_WDI;
- Delay_ms(10);
- if(read_enable==1) //到时间读取模块,间隔时间为1s
- {
- read_data();
- }
- if(receive_finished==1) //接受完成
- {
- led=0;
- receive_finished=0;
- }
- }
- }
- /*******定时中断*********/
- void Timer0(void) interrupt 1
- {
- TH0 = (65535-50000)/256; //50ms
- TL0 = (65535-50000)%256;
- ms++;
- if(ms>=60) //1s
- {
- ms=0;
- read_enable=1;
- }
- }
- /************串行口1中断处理函数*************/
- void UART_1Interrupt() interrupt 4
- {
- //ES=0;
- if(RI)
- {
-
- RI=0;
- RX_Buffer[number] = SBUF;
- number++;
- if(number>36)
- {
- receive_finished=1;
- number=0;
- }
- }
- // ES=1;
- }
- //MS延时函 */
- //****************************************************
- void Delay_ms(unsigned int n)
- {
- unsigned int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<110;j++);
- }
复制代码 |