Код: Выделить всё
#include <avr\io.h>
#include <util\delay.h>
#include "1wire.h"
#include <avr\pgmspace.h>
#include <avr\interrupt.h>
#include <avr\iom8.h>
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr);
void USART_Transmit( unsigned char data );
const uint8_t romm [5] [5] PROGMEM = {
{0x01,0x55,0x44,0x31,0x22},
{0x01,0x55,0x44,0x32,0x22},
{0x01,0x55,0x44,0x33,0x22},
{0x01,0x55,0x44,0x34,0x22},
{0x01,0x55,0x44,0x35,0x22},
};
uint8_t buff[5];
uint8_t match;
uint8_t sit_ocupped = 0;
int main()
{
long i,k;
//cli();
DDRB= 0b00001000;
PORTB=0b00000111;
USART_Init (MYUBRR);
//sei();
while (1)
{
if (! (PINB&(1<<PB1)) ) // кнопка 1 нажата опрашиваем шину 1-wire
{
while (OW_reset())
_delay_ms(200);
OW_write_byte(0x33);
_delay_ms(100);
for (i = 0;i<5;i++)
{
buff[i] = OW_read_byte();
_delay_us(100);
USART_Transmit((buff[i]>>4)+0x30);
USART_Transmit((buff[i]&0x0F)+0x30);
USART_Transmit(32);
}
if (!(PINB & (1 << PB2))) // кнопка 2 нажата сравниваем код
{
uint8_t match = 0;
for (k=0;k<5;k++) {
for (i=0;i<5;i++) {
if (buff[i]!= pgm_read_byte(&romm[i][k]))
{
match=1;
}
}
}
if (match==1) {PORTB |=(1<<PB3);}//если код неравен зажигаем светодиод
else {PORTB &=~(1<<PB3);}
}
}
}
}
void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (0<<URSEL)|(0<<USBS)|(3<<UCSZ0);
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}