波特率4800bps,晶振使用外部晶振11.0592.
主程序t2.c:
//ICC-AVR application builder : 2009-5-6 18:22:23
// Target : M8
// Crystal: 11.059Mhz
#include
#include
#include "ck.h"
void port_init(void)
{
PORTB = 0x01;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00; //有影响
DDRD = 0x60; //有影响,方向设置不对会不准
}
//Watchdog initialize
// prescale: 16K
void watchdog_init(void)
{
WDR (); //this prevents a timeout on enabling
WDTCR |= (1< WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//SPI initialize
// clock rate: 691187hz
void spi_init(void)
{
SPCR = 0x71; //setup SPI
SPSR = 0x00; //setup SPI
}
//UART0 initialize
// desired baud rate: 4800
// actual: baud rate:4800 (0.0%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x8F; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x90;
}
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
//uart has received a character in UDR
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
//watchdog_init();
//timer0_init();
//spi_init();
//uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay(unsigned int ms, unsigned int us)
{
unsigned int i;
if(ms>0)//ms delay
for(;ms;ms--)
for(i=0;i<1000;i++);
if(us>0)//us delay
for(;us>0;us--);
}
main()
{
unsigned int i,j;
init_devices();
PORTB = 0x00;
PORTD = 0x40;
PORTC =0x04;
DDRC |=0x04;
while(1)
{
PORTD ^= 0x60;
delay(900,0);
WByte(0x55);
}
return 0;
}
头文件ck.h:
#include
#define INT8U unsigned char
#define INT16U unsigned int
//模拟串口,波特率4800
void Delay2cp(INT16U i);
void WByte(INT8U input);// ,INT16U ii
INT8U RByte(void);
void TXD0(unsigned char bit)
{
if(bit==0)
PORTC&=0xfB;
else PORTC|=0x04;
}
/*
void delay8(INT8U j)
{
INT8U i;
for(i=0;i}
void delay16(INT16U j)
{
INT16U i;
for(i=0;i}
*/
/*
void serial_initial(void)
{
TMOD = 0x21;
PCON = 0x00;
SCON = 0x58;
RODI = 0;
TH1 = 0xFA; //4800BPS
TL1 = 0xFA;
ES = 1;
SERIAL_INTRPT_ENABLE;
TR1 = 1;
}*/
/* 模拟串口
模拟串口三个函数
*/
//延时程序*
void Delay2cp(INT16U i)
{
unsigned de;
// i = 0x172;
while(i--);
// for(de=0;de<625;de++); //刚好两个指令周期。
}
//void WByte(uchar)
//往串口写一个字节
void WByte(INT8U input)
{
INT8U i = 8;
INT16U ii=0x160;
CLI();
PORTC&=0xfB;//TXD0(0); //发送启始位TXD=0
//Delay2cp(ii);
Delay2cp(ii+100);
//发送8位数据位
while(i--)
{
// TXD0(input&0x01); //先传低位
if((input&0x01)==0)
PORTC &= 0xfB;//output 0(L)
else PORTC |= 0x04;//output 1(H)
Delay2cp(ii);
input = input>>1;
}
Delay2cp(ii);
//发送校验位(无)
PORTC|=0x04;// TXD0(1); //发送结束位 TXD=1
Delay2cp(ii);
SEI();
Delay2cp(ii);
}
阅读(3391) | 评论(0) | 转发(0) |