Hình ảnh của hệ thống

Một phần của tài liệu (LUẬN văn THẠC sĩ) nghiên cứu các chuẩn truyền thông và xây dựng một ứng dụng cho hệ thống giám sát, điều khiển, điều hành tòa nhà cao tầng (Trang 96 - 104)

Phần mã nguồn các phần mềm nhúng điều khiển cho các mạch

1- Phần mã nguồn điều khiển cho mạch Master Chip type : ATmega128

Program type : Application Clock frequency : 16.000000 MHz Memory model : Small

External SRAM size : 0 Data Stack size : 1024

*****************************************************/ #include <mega128.h> #include <delay.h> #include "lcdcustom.c" #include "sw.h" #include "LcdCommand.h" #include "acc.h" #define TX_MODE 1 #define RX_MODE 0

#define RS485_N0_DIR PORTD.7

#define MAX_SID 3 // Max slave IDs #define QUERY_TIMER 1 // N

#define TIMER QUERY_TIMER*10 // N x 16ms // Defines

#define FID_NO 1 #define A_LENGHT 13

unsigned char Dulieu_Floor[A_LENGHT]={'{','O','K',FID_NO,0,0,0,0,0,0,0,0,'}'}; unsigned int count=0;

unsigned char SID_NO; // IDs of Slave 0 -> 31

unsigned char FRAME_Length; // Length of receive frame

#define MAX_LENGTH 20 // Define max length of receive buffer unsigned char R_Buffer[MAX_LENGTH]; // Receive buffer for Slave IDs data union Long_Bytes

{

unsigned long DL_LONG;

unsigned char DL_BYTE[4]; } DULIEU;

bit DATA_Flag=FALSE; // Transmit to master when DATA ready #define UDRE0 UCSR0A.5

#define RXC0 UCSR0A.7 // Declare your global variables here

extern unsigned char STATE_MACHINE; extern flash byte coltech_0[8];

extern flash byte coltech_1[8]; extern flash byte coltech_2[8]; extern flash byte coltech_3[8]; extern flash byte coltech_4[8]; extern flash byte coltech_5[8]; void main(void)

{

while (1) {

// Place your code here switch(STATE_MACHINE) {

case ACC_LOGIN_STATE: ACC_LOGIN_ACTION(); break;

case PASS_LOGIN_STATE: PASS_LOGIN_ACTION(); break;

case CHECK: CHECK_ACTION(); break;

case MONITER: MONITER_ACTION(); break;

} };

}

//=======================================================

// Alphanumeric LCD Module functions #asm

.equ __lcd_port=0x18 ;PORTB #endasm

#include <lcd.h> void Init_uC() { #asm("cli")

PORTA=0xFF; DDRA=0xFF;

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; // Port C initialization PORTC=0xFF; DDRC=0xFF; // Port D initialization PORTD=0xFF; DDRD=0xFF; // Port E initialization PORTE=0xFF; DDRE=0xCF; // Port G initialization PORTG=0x1F; DDRG=0x1F; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 15.625 kHz // Mode: Normal top=FFh // OC0 output: Disconnected ASSR=0x00;

TCCR0=0x07; TCNT0=0x82; // 5 OCR0=0x00;

// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 62.500 kHz // Mode: Normal top=FFFFh // OC1A output: Discon.

OCR1BH=0x00; OCR1BL=0x00; OCR1CH=0x00; OCR1CL=0x00;

// Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh

// OC2 output: Disconnected TCCR2=0x00;

TCNT2=0x00; OCR2=0x00;

// Timer/Counter 3 initialization // Clock source: System Clock

// Clock value: 15.625 kHz // Mode: Normal top=FFFFh // Noise Canceler: Off

// Input Capture on Falling Edge // OC3A output: Discon.

// OC3B output: Discon. // OC3C output: Discon.

// Timer 3 Overflow Interrupt: On // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off // Compare C Match Interrupt: Off

TCCR3A=0x00; TCCR3B=0x05; TCNT3H=0x00; TCNT3L=0x00; ICR3H=0x00; ICR3L=0x00; OCR3AH=0x00; OCR3AL=0x00; OCR3BH=0x00; OCR3BL=0x00; OCR3CH=0x00; OCR3CL=0x00;

// External Interrupt(s) initialization // INT0: Off

// INT1: Off // INT2: Off // INT3: Off // INT4: On

// INT4 Mode: Faling Edge // INT5: Off // INT6: Off // INT7: Off EICRA=0x00; EICRB=0x03; EIMSK=0x10; EIFR=0x10; // USART0 initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity // USART0 Receiver: On

// USART0 Transmitter: On // USART0 Mode: Asynchronous // USART0 Baud rate: 38400

UCSR1B=0xD8; UCSR1C=0x06;

UBRR1H=0x00;

UBRR1L=0x19;

// Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x05;

ETIMSK=0x00;

// Analog Comparator initialization // Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80;

SFIOR=0x00;

// LCD module initialization lcd_init(20);

// Global enable interrupts #asm("sei")

}

//========================================================== // USART0 Transmitter interrupt service routine

interrupt [USART0_TXC] void usart0_tx_isr(void) {

To_SLAVE(); }

//========================================================== // Interrupt to get slave data

// USART0 Receiver interrupt service routine

interrupt [USART0_RXC] void usart0_rx_isr(void) {

static bit FRAME_flag = FALSE; static unsigned char byte_no = 0; unsigned char c; c=UDR0; if(c=='{') { byte_no=0; FRAME_flag=TRUE; } if(FRAME_flag) { R_Buffer[byte_no]=c; if(c=='}') { FRAME_flag=FALSE;

if((R_Buffer[1]=='O') & (R_Buffer[2]=='K')) { FRAME_Length=byte_no+1; //RS485_DATA_ToSend(R_Buffer[3]); DATA_Flag=TRUE; } } else { byte_no++; } } } //==========================================================

if(c!=CHECK_OK[byte_no]) byte_no=0; else { byte_no++; } } else { Command=c; byte_no=0; if(Command=='?') { if(DATA_Flag) To_MASTER(); } } } //======================================================= // Request slave data

#define Q_LENGHT 4

unsigned char QUESTION[Q_LENGHT]={'O','K',0xff,'?'}; void To_SLAVE()

{

static unsigned char byte_no = 0; if(byte_no<Q_LENGHT)

Một phần của tài liệu (LUẬN văn THẠC sĩ) nghiên cứu các chuẩn truyền thông và xây dựng một ứng dụng cho hệ thống giám sát, điều khiển, điều hành tòa nhà cao tầng (Trang 96 - 104)

Tải bản đầy đủ (PDF)

(104 trang)