Trabalho nº 6 Semáforo Rodoviário (automóveis & peões) ******************************************************************** NomeFicheiro: trab6.asm * Data: 3/Junho/2005 * Versão: 1.0 * Autores: José Miguel Gaspar * Olímpia Rodrigues * Orientador: Prof. José Luís Azevedo * Universidade de Aveiro * Seminário EEI 2004/2004 * ******************************************************************** Ficheiro necessário: P16F876.LKR * ******************************************************************** list p=16f876 list directive to define radix decimal processor #include "p16f876.inc" processor specific variable definitions errorlevel -302 Turn off banking message known tested (good) code Constantes RB0 EQU 0 RB1 EQU 1 RB2 EQU 2 RB3 EQU 3 RB4 EQU 4 RB5 EQU 5 RB6 EQU 6 RB7 EQU 7 RA0 EQU 0 RA1 EQU 1 RA2 EQU 2 RA3 EQU 3 RA4 EQU 4 RA5 EQU 5 RA6 EQU 6 RA7 EQU 7 TMR0_SFW_VAL EQU 50 Set Software timer0_count Value TMR0_HW_VAL EQU 60 Set Hardware timer0 Value Constantes de tempo de espera nos estados em unidades de 0,5 seg t_verd2amar_a EQU 3 tempo,s1 = 1,5s t_amar2verm_a EQU 2 tempo,s2 = 1s t_verde_p EQU 8 tempo,s3 = 4s t_verd_interm EQU 6 tempo,s4 = 3s t_verm_a_p EQU 3 tempo,s5 = 1,5s == Definicoes - PORTA == INPUTS SW1 EQU RA4 SW1 está ligado a RA4 Definicoes - PORTB OUTPUTS 1
LED_VERM_A EQU RB1 LED1 está ligado a RB1 LED_AMAR_A EQU RB2 LED2 está ligado a RB2 LED_VERD_A EQU RB3 LED3 está ligado a RB3 LED_VERM_P EQU RB4 LED4 está ligado a RB4 LED_VERD_P EQU RB5 LED5 está ligado a RB5 Variaveis variaveis UDATA 0x20 delay_mult RES 1 Variaveis de controlo dos loops delay_k50 RES 1 da rotina Delay delay_k200 RES 1 conta_verd2amar_a RES 1 conta_amar2verm_a RES 1 conta_verde_p RES 1 conta_verd_interm RES 1 conta_verm_a_p RES 1 estado RES 1 timer0_cnt RES 1 Macros to select the register Banks Bank0 MACRO Macro to select data RAM Bank 0 bcf STATUS,RP0 bcf STATUS,RP1 Bank1 MACRO Macro to select data RAM Bank 1 bsf STATUS,RP0 bcf STATUS,RP1 Bank2 MACRO Macro to select data RAM Bank 2 bcf STATUS,RP0 bsf STATUS,RP1 Bank3 MACRO Macro to select data RAM Bank 3 bsf STATUS,RP0 bsf STATUS,RP1 Macros to select the states macro_inicial MACRO Macro to select state 0 bsf PORTB,LED_VERD_A LED VERDE AUTOMOVEIS ON bcf PORTB,LED_AMAR_A LED AMARELO AUTOMOVEIS OFF bcf PORTB,LED_VERM_A LED VERMELHO AUTOMOVEIS OFF bcf PORTB,LED_VERD_P LED VERDE PEOES OFF bsf PORTB,LED_VERM_P LED VERMELHO PEOES ON macro_verd2amar_a MACRO Macro to select state 1 bsf PORTB,LED_AMAR_A LED AMARELO AUTOMOVEIS ON bcf PORTB,LED_VERM_A LED VERMELHO AUTOMOVEIS OFF bcf PORTB,LED_VERD_P LED VERDE PEOES OFF bsf PORTB,LED_VERM_P LED VERMELHO PEOES ON macro_amar2verm_a MACRO Macro to select state 2 bcf PORTB,LED_AMAR_A LED AMARELO AUTOMOVEIS OFF bsf PORTB,LED_VERM_A LED VERMELHO AUTOMOVEIS ON bcf PORTB,LED_VERD_P LED VERDE PEOES OFF bsf PORTB,LED_VERM_P LED VERMELHO PEOES ON macro_verde_p MACRO Macro to select state 3 bcf PORTB,LED_AMAR_A LED AMARELO AUTOMOVEIS OFF bsf PORTB,LED_VERM_A LED VERMELHO AUTOMOVEIS ON bsf PORTB,LED_VERD_P LED VERDE PEOES ON 2
bcf PORTB,LED_VERM_P LED VERMELHO PEOES OFF macro_verd_interm MACRO Macro to select state 4 bcf PORTB,LED_AMAR_A LED AMARELO AUTOMOVEIS OFF bsf PORTB,LED_VERM_A LED VERMELHO AUTOMOVEIS ON bsf PORTB,LED_VERD_P LED VERDE PEOES ON bcf PORTB,LED_VERM_P LED VERMELHO PEOES OFF macro_verm_a_p MACRO Macro to select state 4 bcf PORTB,LED_AMAR_A LED AMARELO AUTOMOVEIS OFF bsf PORTB,LED_VERM_A LED VERMELHO AUTOMOVEIS ON bcf PORTB,LED_VERD_P LED VERDE PEOES OFF bsf PORTB,LED_VERM_P LED VERMELHO PEOES ON ******************************************************************** ORG 0x0000 Processor reset vector clrf PCLATH Ensure page bits are cleared goto main Go to beginning of program -------------------------------------------------------------------- Rotina Main -------------------------------------------------------------------- ORG 0x0005 main Programa principal. call InitCfg loop do{ btfss INTCON,T0IF while(toif == 0){} goto $-1 bcf INTCON,T0IF T0IF = 0 movlw TMR0_HW_VAL Set Hardware timer0 Value movwf TMR0 Set TMR0 decfsz timer0_cnt,f if(timer0_cnt == 0) movlw d'50' movwf TMR0_SFW_VAL timer0_cnt = 100 while( timer0_cnt!=0 ) switch w = estado sublw 0x00 goto estado_inicial sublw 0x01 goto estado_verd2amar_a sublw 0x02 goto estado_amar2verm_a sublw 0x03 goto estado_verde_p sublw 0x04 goto estado_verd_interm sublw 0x05 nop goto estado_verm_a_p estado_inicial estado inicial macro_inicial btfsc PORTA,SW1 while(sw1 == 0) {} goto estado_inicial 3
estado_verd2amar_a estado_amar2verm_a C:\seminario\guiao_aluno\trab6\trab6.asm incf estado,f estado++ movlw t_verd2amar_a movwf conta_verd2amar_a movlw t_amar2verm_a movwf conta_amar2verm_a movlw t_verde_p movwf conta_verde_p movlw t_verd_interm movwf conta_verd_interm movlw t_verm_a_p movwf conta_verm_a_p goto loop break macro_verd2amar_a decfsz conta_verd2amar_a,f if (count_amar_a == 0) incf estado,f estado++: goto loop break macro_amar2verm_a decfsz conta_amar2verm_a,f if (count_verm_a == 0) incf estado,f estado++: goto loop break estado_verde_p macro_verde_p decfsz conta_verde_p,f if (count_verd_p == 0) incf estado,f estado++: goto loop break estado_verd_interm macro_verd_interm led verde peoes on btfss conta_verd_interm,0 goto off_led bsf PORTB,LED_VERD_P goto fim off_led bcf PORTB,LED_VERD_P fim decfsz conta_verd_interm,f if (count_inter_p == 0) goto loop incf estado,f goto loop estado_verm_a_p macro_verm_a_p decfsz conta_verm_a_p,f if (count_verd_p == 0) clrf estado estado=0 goto loop break =================================================================== InitCfg Configuração inicial dos registros associados ao programa principal Retorna em Bank0 =================================================================== InitCfg ----------------- Analog 2 Digital - InitCfg ---------------------- Bank1 DATA memory (RAM) Bank1 movlw 0x06 W = 0x06 movwf ADCON1 ADCON1 (PORTA I/O digital) ----------------- I/O - InitCfg ----------------------------------- movlw b'0001000' R4 inputs movwf TRISA movlw b'1100000' R[5:0] outputs movwf TRISB R[7:6] inputs ----------------- TRM0 - InitCfg ---------------------------------- clrf OPTION_REG clear OPTION_REG bsf OPTION_REG,PS2 bsf OPTION_REG,PS1 TMR0 Rate bsf OPTION_REG,PS0 1:256 4
Bank0 DATA memory (RAM) Bank0 clrf estado estado = 0 clrf PORTA Inicialização PORTA --------------------------------------------------------------------------- Programação do Timer0 --------------------------------------------------------------------------- movlw TMR0_HW_VAL Set Hardware timer0 Value movwf TMR0 Set TMR0 movlw TMR0_SFW_VAL Set Software timer0_count Value movwf timer0_cnt Save time0_count adjust return END 'end of program' 5