|
|
ช่วยอธิบายโค้ด ภาษา c ควบคุม AVR mega 8 ว่าแต่ละส่วนทำหน้าที่อะไร |
|
|
|
|
|
|
|
ช่วยอธิบายโค้ด ภาษา c ควบคุม AVR mega 8
- อยากทราบหลักการทำงานแต่ละส่วนว่า ส่วนนี้ที่หน้าที่ทำอะไรค่ะ
/*
* AVRGCC9.c
*
* Created: 29/10/2555 22:51:18
* Author: KAKALOS
*/
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <inttypes.h>
#include <stdint.h> // needed for uint8_t
#include <avr/interrupt.h>
#include "lcd.c"
#include "usart.c"
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
uint8_t byteReceived;
uint8_t byteCount = 0;
uint8_t i=0,j,k,p;
uint8_t rfidTag[15] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff} ;
uint8_t data1[15];
uint8_t data2[15];
int main(void)
{
Data1();
Data2();
PortInt();
USART_Init(51);
sei();
lcd_init();
lcd_clr1();
while(1)
{
seedata();
lock1();
lock2();
unlock1();
unlock2();
}
}
ISR(USART_RXC_vect){
byteReceived = UDR;
rfidTag[byteCount] = byteReceived;
byteCount++;
if (rfidTag[11]==data1[11]&& rfidTag[12]==data1[12] && byteCount==16 || rfidTag[11]==data2[11]&& rfidTag[12]==data2[12] &&byteCount==16)
{
i=i^1;
}
if (byteCount==16)
{
byteCount=0;
}
}
void Data1()
{
data1[1] = '5';
data1[2] = '4';
data1[3] = '0';
data1[4] = '0';
data1[5] = '5';
data1[6] = 'B';
data1[7] = '6';
data1[8] = '7';
data1[9] = '2';
data1[10] ='2';
data1[11] ='4';
data1[12] ='A';
}
void clrdata2() {
data2[1] = '0';
data2[2] = '0';
data2[3] = '0';
data2[4] = '0';
data2[5] = '0';
data2[6] = '0';
data2[7] = '0';
data2[8] = '0';
data2[9] = '0';
data2[10] ='0';
data2[11] ='0';
data2[12] ='0';
}
void Data2()
{
data2[1] = '5';
data2[2] = '4';
data2[3] = '0';
data2[4] = '0';
data2[5] = '5';
data2[6] = 'C';
data2[7] = '1';
data2[8] = 'A';
data2[9] = '7';
data2[10] ='A';
data2[11] ='6';
data2[12] ='8';
}
void clrdata1() {
data1[1] = '0';
data1[2] = '0';
data1[3] = '0';
data1[4] = '0';
data1[5] = '0';
data1[6] = '0';
data1[7] = '0';
data1[8] = '0';
data1[9] = '0';
data1[10] ='0';
data1[11] ='0';
data1[12] ='0';
}
void PortInt()
{
DDRC |= _BV(PC0);
DDRC |= _BV(PC1);
DDRC |= _BV(PC2);
DDRC |= _BV(PC3);
DDRC &= ~_BV(PC4);
PORTC |= _BV(PC4);
DDRD &= ~_BV(PD5);
PORTC |= _BV(PC5);
}
void seedata()
{
lcd_command(0xc0);
lcd_putc('I');
lcd_command(0xc1);
lcd_putc('D');
lcd_command(0xc2);
lcd_putc('=');
lcd_command(0xc3);
lcd_putc(rfidTag[1]);
lcd_command(0xc4);
lcd_putc(rfidTag[2]);
lcd_command(0xc5);
lcd_putc(rfidTag[3]);
lcd_command(0xc6);
lcd_putc(rfidTag[4]);
lcd_command(0xc7);
lcd_putc(rfidTag[5]);
lcd_command(0xc8);
lcd_putc(rfidTag[6]);
lcd_command(0xc9);
lcd_putc(rfidTag[7]);
lcd_command(0xca);
lcd_putc(rfidTag[8]);
lcd_command(0xcb);
lcd_putc(rfidTag[9]);
lcd_command(0xcc);
lcd_putc(rfidTag[10]);
lcd_command(0xcd);
lcd_putc(rfidTag[11]);
lcd_command(0xce);
lcd_putc(rfidTag[12]);
lcd_command(0xcf);
lcd_putn(i);
}
void unlock1()
{
if ( data1[1] == rfidTag[1] && data1[2] == rfidTag[2] && data1[3] == rfidTag[3] && data1[4] ==rfidTag[4]&&
data1[5] == rfidTag[5] && data1[6] == rfidTag[6] && data1[7] == rfidTag[7] && data1[8] == rfidTag[8]&&
data1[9] == rfidTag[9] && data1[10] == rfidTag[10] && data1[11] == rfidTag[11] && data1[12] == rfidTag[12]&& i == 0)
{
lcd_clr1();
lcd_command(0x85);
lcd_putc('U');
lcd_command(0x86);
lcd_putc('N');
lcd_command(0x87);
lcd_putc('L');
lcd_command(0x88);
lcd_putc('O');
lcd_command(0x89);
lcd_putc('C');
lcd_command(0x8a);
lcd_putc('K');
lcd_command(0x8b);
lcd_putc('!');
PORTC |= _BV(PC0);
PORTC &= ~_BV(PC1);
PORTC &= ~_BV(PC3);
if((PINC & _BV(PC4)) == 0)
{
PORTC &= ~_BV(PC2);
Data2();
Data1();
}
else
{
PORTC |= _BV(PC2);
}
}
}
void unlock2()
{
if ( data2[1] == rfidTag[1] && data2[2] == rfidTag[2] && data2[3] == rfidTag[3] && data2[4] ==rfidTag[4]&&
data2[5] == rfidTag[5] && data2[6] == rfidTag[6] && data2[7] == rfidTag[7] && data2[8] == rfidTag[8]&&
data2[9] == rfidTag[9] && data2[10] == rfidTag[10] && data2[11] == rfidTag[11] && data2[12] == rfidTag[12] && i == 0)
{
lcd_clr1();
lcd_command(0x85);
lcd_putc('U');
lcd_command(0x86);
lcd_putc('N');
lcd_command(0x87);
lcd_putc('L');
lcd_command(0x88);
lcd_putc('O');
lcd_command(0x89);
lcd_putc('C');
lcd_command(0x8a);
lcd_putc('K');
lcd_command(0x8b);
lcd_putc('!');
PORTC |= _BV(PC0);
PORTC &= ~_BV(PC1);
PORTC &= ~_BV(PC3);
if((PINC & _BV(PC4)) == 0)
{
PORTC &= ~_BV(PC2);
Data2();
Data1();
}
else
{
PORTC |= _BV(PC2);
}
}
}
void lock1()
{
if (
data1[1] == rfidTag[1] && data1[2] == rfidTag[2] && data1[3] == rfidTag[3] && data1[4] ==rfidTag[4]&&
data1[5] == rfidTag[5] && data1[6] == rfidTag[6] && data1[7] == rfidTag[7] && data1[8] == rfidTag[8]&&
data1[9] == rfidTag[9] && data1[10] == rfidTag[10] && data1[11] == rfidTag[11] && data1[12] == rfidTag[12]&& i == 1 )
{
lcd_clr1();
lcd_command(0x86);
lcd_putc('L');
lcd_command(0x87);
lcd_putc('O');
lcd_command(0x88);
lcd_putc('C');
lcd_command(0x89);
lcd_putc('K');
lcd_command(0x8a);
lcd_putc('!');
PORTC |= _BV(PC1);
PORTC &= ~_BV(PC0);
PORTC &= ~_BV(PC2);
if((PINC & _BV(PC5)) == 0)
{
PORTC &= ~_BV(PC3);
}
else
{
clrdata2();
Data1();
PORTC |= _BV(PC3);
}
}
}
void lock2()
{
if( data2[1] == rfidTag[1] && data2[2] == rfidTag[2] && data2[3] == rfidTag[3] && data2[4] ==rfidTag[4]&&
data2[5] == rfidTag[5] && data2[6] == rfidTag[6] && data2[7] == rfidTag[7] && data2[8] == rfidTag[8]&&
data2[9] == rfidTag[9] && data2[10] == rfidTag[10] && data2[11] == rfidTag[11] && data2[12] == rfidTag[12] && i== 1)
{
lcd_clr1();
lcd_command(0x86);
lcd_putc('L');
lcd_command(0x87);
lcd_putc('O');
lcd_command(0x88);
lcd_putc('C');
lcd_command(0x89);
lcd_putc('K');
lcd_command(0x8a);
lcd_putc('!');
PORTC |= _BV(PC1);
PORTC &= ~_BV(PC0);
PORTC &= ~_BV(PC2);
if((PINC & _BV(PC5)) == 0)
{
PORTC &= ~_BV(PC3);
}
else
{
clrdata1();
Data2();
PORTC |= _BV(PC3);
}
}
}
Tag : C
|
|
|
|
|
|
Date :
2013-05-01 17:06:32 |
By :
parichatlek |
View :
1043 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยหน่อยนะค่ะ พอดีไม่ค่อยเข้าใจค่ะ
|
|
|
|
|
Date :
2013-05-01 17:11:45 |
By :
parichatlek |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยหน่อยค่ะ จะขอบคุณมากๆ เรย พอดีเป็นโปรเจคของเราเอง
|
|
|
|
|
Date :
2013-05-01 17:32:54 |
By :
parichatlek |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|