?? main.c
字號(hào):
#include <avr/io.h>
#include <avr/interrupt.h>
#ifndef F_CPU
//define cpu clock speed if not defined
#define F_CPU 8000000
#endif
//set desired baud rate
#define BAUDRATE 9600
//calculate UBRR value
#define UBRRVAL ((F_CPU/(BAUDRATE*16UL))-1)
#include <util/delay.h>
//int out_temp;
//out_temp=0;
int num595;
void
lockdata (void)
{
PORTB &= ~(1 << PB0);
_delay_us (1);
PORTB |= 1 << PB0;
_delay_us (1);
}
void
loopback595 (void)
{
if (PINC & 1)
PORTB |= 1 << PB3;
else
PORTB &= ~(1 << PB3);
PORTB |= (1 << PB5);
_delay_us (1);
PORTB &= ~(1 << PB5);
_delay_us (1);
}
int
writebit595 (int data)
{
if (data & 1)
PORTB |= 1 << PB3;
else
PORTB &= ~(1 << PB3);
_delay_us (1);
PORTB |= (1 << PB5);
_delay_us (1);
PORTB &= ~(1 << PB5);
_delay_us (1);
//lockdata();
return PINC & 1;
}
int
writeBit595 (int data)
{
int i, result;
result = 0;
for (i = 0; i < 8; i++)
{
result = result << 1;
result += writebit595 ((data >> i) & 1);
}
//result=result/2;
//PORTC=result;
//lockdata();
return result & 0xff;
}
int
count_595port (void)
{
int result;
result = 0;
while (!(writeBit595 (0xaa) == 0xaa))
{
result++;
}
return result;
}
void
clear595 (void)
{
int i;
if (num595 != 0)
{
for (i = 0; i < num595; i++)
writeBit595 (0);
}
else
{
count_595port ();
for (i = 0; i < num595; i++)
writeBit595 (0);
}
}
ISR (SIG_UART_RECV)
{
uint8_t Temp;
while (!(UCSRA & (1 << RXC)))
{
};
Temp = UDR;
int i;
PORTC = Temp;
if (Temp == 0xff)
{
for (i = 0; i < (num595 * 8); i++)
{
writebit595 (1);
}
}
else if (Temp == 0)
clear595 ();
else
{
for (i = 1; i < (num595 * 8 + 1); i++)
{
if (i == (Temp >> 1))
writebit595 (Temp & 1);
else
loopback595 ();
}
}
lockdata ();
//while (!(UCSRA&(1<<UDRE))){};
// if(UCSRA&(1<<UDRE))
//send received data back
// UDR=Temp;
}
void
init (void)
{
//Set baud rate
UBRRL = UBRRVAL; //low byte
UBRRH = (UBRRVAL >> 8); //high byte
UCSRC = (1 << URSEL) | (0 << UMSEL) | (0 << UPM1) | (0 << UPM0) |
(0 << USBS) | (0 << UCSZ2) | (1 << UCSZ1) | (1 << UCSZ0);
UCSRB = (1 << RXCIE) | (0 << TXCIE) | (1 << RXEN) | (1 << TXEN);
sei ();
}
int
main (void)
{
DDRB = 0xff;
PORTB = 0x01;
DDRC = 0xfe;
num595 = 0;
num595 = count_595port ();
PORTC = num595 << 1;
clear595 ();
init ();
int n;
n = 0;
while (1);
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -