📄 csbmain.c
字号:
//ICC-AVR application builder : 2008-1-25 14:03:04
// Target : m48
// Crystal: 7.3728Mhz
//finish time 08/01/25
#include <iom48v.h>
#include <macros.h>
unsigned int count_sum;//计时值
unsigned char s_celiang,send_socr;
unsigned char buff[6]={0,0,0,'.',0,'M'};
unsigned long c_sum;
void port_init(void)
{
PORTB = 0x00;// __
DDRB = 0x02;//pb1__| |__
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1
// WGM: Normal
// desired value: 40KHz
// actual value: 40.070KHz (0.2%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0xD2; //set count
TCCR0A = 0x00;
TCCR0B = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)//波形产生
{
//TIMER0 has overflowed
PORTB = (~PORTB)&0x02;// 取反pb1电平
TCNT0 = 0xD2; //set count
//TCCR0B = 0x01; //start timer
}
void send_to_pc()//数据回传给PC
{
unsigned char i;
for(i=0;i<6;i++)
{
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0=buff[i];
}
}
//TIMER1 initialize - prescale:8
// 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*/;
TCCR1A = 0x00;
//start Timer
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x00 /*INVALID SETTING*/; //reload counter high value
TCNT1L = 0x00 /*INVALID SETTING*/; //reload counter low value
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x2F; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x08;
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
//external interupt on INT0
s_celiang++;
if(s_celiang==5) TCCR1B = 0x02;//启动计数
if(s_celiang==6)
{
TCCR0B = 0x00; //start timer
TCCR1B = 0;//停止计数
count_sum=TCNT1;
TCNT1=0;//清空计数
s_celiang=0;
send_socr=1;
TCCR0B = 0x02; //start timer
}
}
void mux_sum(unsigned long sum)//计算距离
{
unsigned long x;
x=sum/100;
buff[0]=x+0x30;//百位
x=sum%100/10;
buff[1]=x+0x30;//十位
x=(char) sum%100%10;
buff[2]=x+0x30;//个位
x=(char) sum*10%100%10;
buff[4]=x+0x30;//小数
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
timer1_init();
uart0_init();
MCUCR = 0x00;
EICRA = 0x02; //extended ext ints
EIMSK = 0x01;
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x01; //timer 1 interrupt sources
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1)
{
if(send_socr)
{
c_sum=count_sum;
c_sum=(c_sum*340/2)/1000000;
mux_sum(c_sum);
send_to_pc();
send_socr=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -