📄 uart_main._c
字号:
// ICC-AVR application builder : 2007-11-28 8:53:26
// Target : M48
// Crystal: 8.0000Mhz
#include <iom48v.h>
#include <macros.h>
#include "main.h"
#include "modbus.h"
#include "timer2.h"
#include "led.h"
void test(void)
{
// LedShowLink = 5678;
}
void delay(uint num)
{
while(num-->0)
;
}
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
#pragma interrupt_handler timer2_ovf_isr:10
void timer2_ovf_isr(void)
{
TCNT2 = 0x83; //reload counter value
timeBase.bits.bt1ms = 1;
}
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
uint8 tmp;
receTimeOut = 10; //通讯超时值
//PORTC ^= 0x02;
if(UCSR0A&BIT(FE0) != 0) //帧错. //if(FERR==1)
{
UCSR0B &= ~BIT(RXEN0); //CREN = 0;
UCSR0A &= ~BIT(FE0); //FERR = 0;
UCSR0B |= BIT(RXEN0); //CREN = 1;
tmp = UDR0;
//PORTC ^= 0x02;
}
//else if(OERR==1)
else if(UCSR0A&BIT(DOR0)) //溢出错.
{
UCSR0B &= ~BIT(RXEN0); //CREN = 0;
UCSR0A &= ~BIT(DOR0); //OERR = 0;
UCSR0B |= BIT(RXEN0); //CREN = 1;
tmp = UDR0;
//PORTC ^= 0x02;
}
else //奇偶校验的问题.
{
// RX9DBuf = RX9D;
// 读取接受寄存器中的数据.
receBuf[receCount] = UDR0; // RCREG;
// 奇校验判断.
//if(RX9DBuf != ParityBit(&receBuf[receCount]))
if(UCSR0A&BIT(UPE0)!=0) //奇校验硬件完成.
{
//PORTC ^= 0x02;
UCSR0A |= BIT(UPE0);
checkoutError = 1;
}
receCount++; // 接收地址偏移寄存器加1.
receCount &= 0x0f; // 最多接受16个字节.
// PORTC ^= 0x02;
} //
} //void uart0_rx_isr(void)
#pragma interrupt_handler uart0_udre_isr:21
void uart0_udre_isr(void)
{
//character transferred to shift register so UDR is now empty
// UDR0 = 0x01;
if(sendPosi<sendCount)
{
sendPosi++;
// TX9D = ParityBit(sendBuf[sendPosi]);
UDR0 = sendBuf[sendPosi];
}
else
{
// receEnable(); //485芯片
if(UCSR0A&BIT(TXC0) != 0) //if(TRMT)
{
receCount = 0;
checkoutError = 0;
//--------------------------
//UCSR0B &= ~BIT(UDRIE0); //RXCIE0 清零. TXIE = 0;//在一组数据发送完成后关闭
UCSR0B &= ~BIT(TXCIE0);
//TXEN = 0;
UCSR0B &= ~BIT(TXEN0);
sendCount = 0;
//--------------------------
// flag_bit = 0; //&&&&&&&&
}
}
}
void test_pin(void)
{
DDRC |= 0x02;
PORTC |= 0x02;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
timer2_init();
led_init();
test_pin();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x01; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x84; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
NOP();
init_devices();
NOP();
singleKeyTranmit();
delay(50000);
singleKeyTranmit();
test();
while(1)
{
ReceiveFrameDisFun();
//UDR0 = 0x01;
if(timeBase.bits.bt1ms)
time_mission();
if(ledControl.bits.once128ms) // if(var_a == 1) //
{
//PORTC ^= 0x02;
ledControl.bits.once128ms=0; //var_a = 0; //
sub2();
}
if(ledControl.bits.refurbish) //if(var_b == 1) //
{
//PORTC ^= 0x02;
ledControl.bits.refurbish=0;//var_b = 0; //
subled();
}
}//while(1)
}//void main(void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -