📄 uart.c
字号:
#include <iom8v.h>
#include "Includes.h"
#include"uart.h"
#define fosc 8000000 //晶振8.0000MHZ
#define baud 4800 //波特率
#define maxdata 20000 //采集的最大数值
#define voltagedata 0x01 //电压伏值 3个字节
#define currentdata 0x02 //电流伏值 3个字节
#define entercomm 0x56 //确认帧 0字节
#define addressper 0x10 //本机地址
// LOCK status definitions
#define LOCK_NOK 0x00
#define LOCK_OK 0x01
#define LOCK_RECAL_OK 0x02
#define uchar unsigned char
// Maximum number of re-calibration attempts upon calibration failure
#define CAL_ATTEMPT_MAX 4
// Time-out values
#define CAL_TIMEOUT 0x7FFE
#define LOCK_TIMEOUT 0x7FFE
const unsigned char DefaultConfig433[32] =
{
0x0F, // 0x01, INTERFACE
0xFF, // 0x02, RESET
0x8F, // 0x03, SEQUENCING
0x3A, // 0x04, FREQ_2A
0x10, // 0x05, FREQ_1A
0x01, // 0x06, FREQ_0A
0x39, // 0x0B, CLOCK_A 19.2K
0x3A, // 0x08, FREQ_2B
0x1A, // 0x09, FREQ_1B
0xAB, // 0x0A, FREQ_0B
0x39, // 0x0B, CLOCK_A 19.2K
0x44, // 0x0C, VCO
0x50, // 0x0D, MODEM
0xbb, // 0x0E, DEVIATION
0x46, // 0x0F, AFC_CONTROL
0x25, // 0x10, FILTER
0x61, // 0x11, VGA1
0x55, // 0x12, VGA2
0x2d, // 0x13, VGA3
0x29, // 0x14, VGA4
0x20, // 0x15, LOCK
0x78, // 0x16, FRONTEND
0x47, // 0x17, ANALOG, RX=47/TX=47
0x14, // 0x18, BUFF_SWING
0x22, // 0x19, BUFF_CURRENT
0xAE, // 0x1A, PLL_BW
0x34, // 0x1B, CALIBRATE
0x0f, // 0x1C, PA_POWER
0x00, // 0x1D, MATCH
0x00, // 0x1E, PHASE_COMP
0x00, // 0x1F, GAIN_COMP
0x00 // 0x20, POWERDOWN
};
unsigned char boolval,retrn_val,looptmp;
char bool_val;
char PA_POWER=0xF0;
char RXANALOG=0x47;
char TXANALOG=0x47;
uchar rcv1[50];
uchar rcvd1,rscnt11,rscnt12,rscnt13,rscnt01,xx0,xx1;
uchar rsok00,rsok10,rsok20,rsok30,rsok40;
//extern unsigned char read_flash(unsigned int add);
uchar w1;
uchar databak[3],datawn2[3];
/* 串口通讯变量定义 */
uchar Tx_Rx,Rx_Time;
/*--------------------------------------------------------------------------------------------------
变量定义
--------------------------------------------------------------------------------------------------*/
//********量程切换********//
INT8U Rx_Time; // 通讯间隔时间
/* 数字滤波 */
INT16U Vwn_Buf[13];
/*--------------------------------------------------------------------------------------------------
函数定义
--------------------------------------------------------------------------------------------------*/
void Initial ( void )
{
Rx_Time = 0;// 通讯间隔时间
}
//Watchdog initialize
// prescale: 2048K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}
/* 字符输出函数 */
void putchar(uchar c)
{
while (!(UCSRA&(1<<UDRE)));
UDR=c;
}
//TIMER0 initialize - prescale:256
// desired value: 5mSec
// actual value: 4.965mSec (0.7%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x71; //set count
TCCR0 = 0x04; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x71; //reload counter value
//AD转换
WDR();// 喂狗
}
//TIMER1 initialize - prescale:Rising edge
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: Out of range
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
OCR1AH = 0x00 /*INVALID SETTING*/;
OCR1AL = 0x00 /*INVALID SETTING*/;
OCR1BH = 0x00 /*INVALID SETTING*/;
OCR1BL = 0x00 /*INVALID SETTING*/;
ICR1H = 0x00 /*INVALID SETTING*/;
ICR1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
TCCR1B = 0x06; //start Timer
}
//TIMER2 initialize - prescale:32
// WGM: Normal
// desired value: 1mSec
// actual value: 0.998mSec (0.2%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x1A; //setup
OCR2 = 0xE6;
TCCR2 = 0x03; //start
}
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
TCNT2 = 0x1A; //reload counter value
/* 通讯数据间隔判断*/
if(Tx_Rx == 0) // 通讯
{
if(++Rx_Time > 40) // 间隔时间大于40ms后判断为接收超时
{
Tx_Rx = 2; // 接收结束;
Rx_Time = 0;
rsok00=0;
rsok10=0;
rsok20=0;
rsok30=0;
rsok40=0;
rscnt01=0;
rscnt11=0;
rscnt12=0x00;
rscnt13=0x00;
}
else
{}
}
}
#pragma interrupt_handler int1_isr:3
void int1_isr(void)
{
//external interupt on INT1
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
watchdog_init();
timer0_init();
timer1_init();
timer2_init();
MCUCR = 0x08; //INT1的下降沿产生中断
GICR = 0x80; //BOOT区起始地址
TIMSK = 0x41; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//串行接收中断服务程序
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
if(!(UCSRA& (1<<RXC)))
return;
if(rsok00==0){
rcvd1=UDR;
if(rcvd1==0x55)rscnt01++;
else if(rcvd1==0xaa)rscnt01++;
else{}
if((rcvd1==0xaa)&&(rscnt01>=1)){rsok00=1;
rscnt01=0;
}
}
else{
if(rsok10==0){
rscnt11=UDR;
// if((rscnt11==addressper)||(rscnt11==0x00))
if(rscnt11==addressper)
{ rsok10=1;
xx0=0;xx1=0;
xx0=xx0^0x55;
xx0=xx0^0xaa;
xx0=xx0^rscnt11;
Tx_Rx=0;
}
else { rsok00=0;}
}
else{
if(rsok20==0){
rscnt12=UDR;
rsok20=1;
xx0=xx0^rscnt12;
}
else {
if(rsok30==0){
rscnt13=UDR;
rsok30=1;
xx0=xx0^rscnt13;
}
else{
rcv1[rscnt01]=UDR;
if(rscnt01<(rscnt13+1)){ xx0=xx0^rcv1[rscnt01];
xx1+=rcv1[rscnt01];
}
if(rscnt01==(rscnt13+1)){
if(xx0==rcv1[rscnt01])
{ rsok40=1;
Rx_Time = 0;
Tx_Rx = 2;
}
else {
// UCSRB=UCSRB|0x08;
rsok00=0;
rsok10=0;
rsok20=0;
rsok30=0;
rsok40=0;
rscnt01=0;
rscnt11=0;
Tx_Rx=2;
Rx_Time=0;
}
}
rscnt01++;
}
}
}
}
}
void delay(unsigned long delay_count)
{
unsigned char loopi;
while(delay_count-- != 0)
{
for(loopi=0;loopi<2;loopi++);
}
}
/* 字符串输出函数 */
void puts(char *s)
{
while (*s)
{
putchar(*s);
s++;
}
putchar(0x0a);//回车换行
putchar(0x0d);
}
/* UART初始化 */
void uart_init(void)
{
// OSCCAL=read_flash(0x1f20);
rsok00=0;
rsok10=0;
rsok20=0;
rsok30=0;
rsok40=0;
rcvd1=0x00;
rscnt11=0x00;
rscnt12=0x00;
rscnt13=0x00;
rscnt01=0x00;
xx0=0;xx1=0;
//UCSRB=0x90;//允许发送并开接收中断,发送不使能
UCSRB = 0x00;
UCSRA = 0x00;
UBRRL=(fosc/16/(baud+1))%256;
UBRRH=(fosc/16/(baud+1))/256;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP位
UCSRB=0x98;
}
void port_init(void)
{
//PB1-PCLK
//PB0-PDIO
PORTB = 0x00;
DDRB = 0x03;
//PD5-PSEL
//PD3-DCLK
//PD4-DIO
PORTD = 0x20;
DDRD = 0x30;
}
/* 确认帧 */
void entcomm(void)
{
xx0=0;xx1=0;
UDR=0x55;
xx0=xx0^0x55;
xx1+=0x55;
while (!(UCSRA&(1<<UDRE)));
UDR=0xaa;
xx0=xx0^0xaa;
xx1+=0xaa;
while (!(UCSRA&(1<<UDRE)));
UDR=addressper;
xx0=xx0^addressper;
xx1+=addressper;
while (!(UCSRA&(1<<UDRE)));
UDR=0x56;
xx0=xx0^0x56;
xx1+=0x56;
while (!(UCSRA&(1<<UDRE)));
UDR=0;
xx0=xx0^0x00;
xx1+=0x00;
while (!(UCSRA&(1<<UDRE)));
UDR=0x16;
xx0=xx0^0x16;
xx1+=0x16;
while (!(UCSRA&(1<<UDRE)));
UDR=xx0;
while (!(UCSRA&(1<<UDRE)));
}
/* 发送交流电压值 */
void voltagerepol(void)
{
xx0=0;xx1=0;
UDR=0x55;
xx0=xx0^0x55;
xx1+=0x55;
while (!(UCSRA&(1<<UDRE)));
UDR=0xaa;
xx0=xx0^0xaa;
xx1+=0xaa;
while (!(UCSRA&(1<<UDRE)));
UDR=addressper;
xx0=xx0^addressper;
xx1+=addressper;
while (!(UCSRA&(1<<UDRE)));
UDR=0x01;
xx0=xx0^0x01;
xx1+=0x01;
while (!(UCSRA&(1<<UDRE)));
UDR=0x03;
xx0=xx0^0x03;
xx1+=0x03;
while (!(UCSRA&(1<<UDRE)));
for(w1=0;w1<3;w1++)
{
UDR=databak[w1];
xx0=xx0^databak[w1];
xx1+=databak[w1];
while (!(UCSRA&(1<<UDRE)));
}
UDR=0x16;
xx0=xx0^0x16;
xx1+=0x16;
while (!(UCSRA&(1<<UDRE)));
UDR=xx0;
while (!(UCSRA&(1<<UDRE)));
}
/* 发送交流电流值 */
void currentrepol(void)
{
xx0=0;xx1=0;
UDR=0x55;
xx0=xx0^0x55;
xx1+=0x55;
while (!(UCSRA&(1<<UDRE)));
UDR=0xaa;
xx0=xx0^0xaa;
xx1+=0xaa;
while (!(UCSRA&(1<<UDRE)));
UDR=addressper;
xx0=xx0^addressper;
xx1+=addressper;
while (!(UCSRA&(1<<UDRE)));
UDR=0x02;
xx0=xx0^0x02;
xx1+=0x02;
while (!(UCSRA&(1<<UDRE)));
UDR=0x03;
xx0=xx0^0x03;
xx1+=0x03;
while (!(UCSRA&(1<<UDRE)));
for(w1=0;w1<3;w1++)
{
UDR=databak[w1];
xx0=xx0^databak[w1];
xx1+=databak[w1];
while (!(UCSRA&(1<<UDRE)));
}
UDR=0x16;
xx0=xx0^0x16;
xx1+=0x16;
while (!(UCSRA&(1<<UDRE)));
UDR=xx0;
while (!(UCSRA&(1<<UDRE)));
}
unsigned char ReadFromCC1020Register(unsigned char addr)
{
unsigned char itmp;
unsigned char tmp;
// P2MDOUT = 0xE0;
DDRD = 0x30;
DDRB = 0X03;
delay(10);
PSEL_0;
itmp = (addr&0x7f) <<1 ;
for(tmp=8;tmp>0;tmp--){
Pclk_0;
if((itmp&0x80) == 0x80) PData_1;
else PData_0;
delay(10);
Pclk_1;
delay(10);
itmp = itmp<<1;
}
itmp = 0;
// P2MDOUT = 0xA0;
PData_1;
DDRB = 0X02;
for(tmp=8;tmp>0;tmp--){
itmp =itmp <<1;
Pclk_0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -