📄 main.c
字号:
//ICC-AVR application builder : 2007-4-24 9:11:17
// Target : M88
// Crystal: 8.0000Mhz
#include <iom88v.h>
#include <macros.h>
#include <uart.c>
#include <timer1.c>
//#include <stdlib.h>
//extern char _bss_end;
//ADC initialize
// Conversion time:128 prescale 208uS,free running,AVCC
//unsigned char AdcIntrCnt = 0;
/*void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x42; //select adc input 2,REF = AVCC
ACSR = 0x80;
ADCSRB = 0x00;
DIDR0 = 0x04; //禁用ADC2数字输入缓冲
ADCSRA = 0xEF;
}*/
/*#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
//conversion complete, read value (int) using...
int value;
int pressure;
char *ptr;
float cal;
value=ADCL; //Read 8 low bits first (important)
value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
if(++AdcIntrCnt > 100)
{
// PORTB = PORTB ^( 1<<PB3); //debug
AdcIntrCnt = 0;
// putchar(ADCL); //debug
// putchar(ADCH); //debug
// 进行相应转换,并发送
// if(value > 204) //电压应该大于1v
{
cal = value * 20/ 1023; //Ima,R = 250ohm
cal = (cal - 4) / 16 * 350 ; //pressure/bar
pressure = (int)cal;
ptr = (char*)&pressure;
putchar(*ptr); //传送压力低字节
putchar(*(ptr + 1)); //传送压力高字节
}
}
}*/
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x1E;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x08;
DDRD = 0x38;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
uart0_init();
//timer0_init();
//adc_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x06; //timer 0 interrupt sources
TIMSK1 = 0x07; //timer 1 interrupt sources
TIMSK2 = 0x00; //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 = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
unsigned int waitFlag = 0; //串口通讯等待时间计数
unsigned char frameLength = 12; //帧长度
char i = 0;
// _NewHeap(&_bss_end + 1,&_bss_end+40);
//初始化外围器件
init_devices();
while(1)
{
if(FrameHead0 > 0) //帧接收中
{
TCCR1B = 0x00; //关定时器1,准备接收数据
//ADCSRA = ADCSRA & ~(1<<ADIE); //disable adc interrupt
putchar(IDLE); //为和数据区分开,采用两字节的控制命令
putchar(IDLE);
i = 1;
while(i)
{
if(rx_counter0 == frameLength) //缓冲区收到规定字符数
{
//处理缓冲区数据
DisposeBufDatas();
// FlashRxBuf(); //debug
waitFlag = 0;
timer1_init(); //开定时器
//ADCSRA = ADCSRA | (1<<ADIE); //enable adc interrupt
i = 0;
}
else
{
waitFlag++;
if(waitFlag == 0xFFFF) //等待超时,刷新缓冲区
{
waitFlag = 0;
FlashRxBuf();
putchar(250); //回传超时标志 debug
putchar(250); //回传chongfa曛
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -