本帖最后由 索玥嘟嘟 于 2019-12-16 09:11 编辑
谢谢回复~~~帮我看下程序逻辑吧,我实在分析不出来了,点灯程序我放到串口中断和主函数中都是不稳定的
1、这是422串口中断接收程序
/***************串口1计轴设备中断接收*******************/
#pragma interrupt_handler Rx1_isr:33
void Rx1_isr(void)
{
unsigned char temp, i;
unsigned int tempii;
temp = UDR1;
if ((temp == 0xFE) && (Rx1BFlag == Start))
{
Rx1Flag = Start;
receive1_no = 2;
Rx1BFlag = 0;
}
else
{
if (temp == 0xFB) /*帧头检测*/ //接收18个字节 帧头为FB FE
{
Rx1BFlag = Start;
}
else
Rx1BFlag = 0;
if (Rx1Flag == Start) /*接收数据*/
{
JzStatA[receive1_no] = temp;
receive1_no++;
if (receive1_no >= 18)
{
receive1_no = 0;
JzStopCount = 0;
Rx1Flag = Finish;
JZLedCount = 0; //点通讯灯
PORTD &= ~(1 << PD4); //点通讯灯PD4
for (i = 0; i < 16; i++)
{
JZ_crc_buffer = JzStatA[i + 2];
}
}
}
}
}
2、定时器中点灯计数的程序:
/*********T/C1比较中断**********/
#pragma interrupt_handler timer1_comp_isr:13
void timer1_comp_isr(void) //8.139ms
{
if (JZLedCount++ >= LEDTime)
{
JZLedCount = LEDTime;
PORTD |= (1 << PD4); //422的LED灯灭 PD4
}
}
3、定时器的初始化和中断程序
/***************** 初始化定时器1 *****************/
void init_Timer1(void)
{
TCCR1B = 0x00; //关闭定时器1
OCR1AH = 0x1D;
OCR1AL = 0x4C; //8.139ms 7.3728M
TCNT1H = 0x00;
TCNT1L = 0x00; //计数初值设为零
TCCR1A = 0x00; //比较匹配A输出
TCCR1C = 0x00; //比较匹配时触发OC3A,OC3X为原OC3X取反
TCCR1B = 0x0A; //启动定时器1,8分频,CTC模式
TIMSK1 = 0x02; //timer1比较匹配中断使能
/***************定时器1中断寄存器配置**************/
void InitIER(void)//2015.4.7
{
SMCR = 0x00;
EICRA = 0x00;
EICRB = 0x00;//INT4低电平沿触发0x00//上升沿0x03//下降沿0x02
EIMSK = 0x10;//外部中断4使能 1CAN-INT or 2CAN-INT
EIFR = 0xFF;
}
4、串口1的初始化
void ini_USART1(void)
{ // desired baud rate:9600
UBRR1L = 47; //7.3728M:47
UBRR1H = 0x00;
UCSR1A = 0x00;
UCSR1B = 0x98;
UCSR1C = 0x06;
PORTD &= ~(1 << PD1); //打开发送
}
|