📄 leson2._c
字号:
#include <io2313v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
//#define LED_SET PORTD |= (1 << PD5) //位置高
//#define LED_CLR PORTD &= ~(1 << PD5) //位置低
//#define LED_COM PORTD ^= (1 << PD5) //位取反
//#define LED_R PIND & (1 << PD5) //位读取
void WDT_off(void)
{
//__disable_interrupt();
//__watchdog_reset();
//#asm("cli");
SREG &= ~(1 << (SREG&0x80));
// Clear WDRF in MCUSR
MCUSR &= ~(1<<(MCUSR&0x08));
// Write logical one to WDCE and WDE
// Keep old prescaler setting to prevent unintentional time-out
WDTCSR |= (1<<(WDTCSR&0x10)) | (1<<(WDTCSR&0x08)WDE);
// Turn off WDT
WDTCSR = 0x00;
SREG |= (1 << (SREG&0x80));
//#asm("sei");
//__enable_interrupt();
}
void USART_Init(unsigned int baud)
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<(UCSRB&0x10))|(1<<(UCSRB&0x08));
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<(UCSRC&0x08))|(3<<(UCSRC&0x02));
//U2X=0;
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<(UCSRA&0x20))) );
/* Put data into buffer, sends the data */
UDR = data; //Transmit 8 data bits
}
/*
void USART_Transmit( unsigned int data )
{
//Wait for empty transmit buffer
while (!( UCSRA & (1<<UDRE)));
// Copy 9th bit to TXB8
UCSRB &= ~(1<<TXB8);
if ( data & 0x0100 )
UCSRB |= (1<<TXB8);
// Put data into buffer, sends the data
UDR = data; //Transmit 9 data bits
}
*/
unsigned char USART_Receive( void )
{
// Wait for data to be received
while ( !(UCSRA & (1<<(UCSRA&0x80))) );
//Get and return received data from buffer
return UDR; //Receive 8 data bits
}
/*
unsigned int USART_Receive( void )
{
unsigned char status, resh, resl;
// Wait for data to be received
while ( !(UCSRA & (1<<RXC)) );
// Get status and 9th bit, then data
// from buffer
status = UCSRA;
resh = UCSRB;
resl = UDR;
// If error, return -1
if ( status & (1<<FE)|(1<<DOR)|(1<<UPE) )
return -1;
// Filter the 9th bit, then return
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl); //Receive 9 data bits
}
*/
void delay(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
void senddata(uint data)
{
uchar i;
for(i=0;i<15;i++)
{
PORTD&=~(1<<(PORTD&0x10));
NOP();
if(data&0x8000)
PORTD|=(1<<(PORTD&0x20)); //置位操作
else
PORTD&=~(1<<(PORTD&0x20)); //清零操作
PORTD|=(1<<(PORTD&0x10));
NOP();
data=data<<1;
}
PORTD|=(1<<(PORTD&0x08));
NOP();
NOP();
NOP();
PORTD&=~(1<<(PORTD&0x08));
//PORTD|=(1<<PORTD5); //置位操作
//PORTD&=~(1<<PORTD5); //清零操作
}
void main()
{
uchar i;
uint j=0x0001;
uint k=0x7fff;
DDRD=0x38;
WDT_off();
USART_Init(9600);
while(1)
{
USART_Transmit(0x55);
for(i=0;i<15;i++)
{
senddata(j);
delay(200);
j=j<<1;
j=j+1;
}
for(i=0;i<15;i++)
{
senddata(k);
delay(200);
k=k>>1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -