Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Traffic light controller using PIC16f877a problem

Status
Not open for further replies.

oday_albadarneh

Newbie level 6
Joined
Dec 20, 2011
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,380
Dear all,
My friend have an assignment in the Micro-controllers course, he has to write an assembly code to control a traffic light using PIC16f877a attached to a 7-segment display s the following flow chart
View attachment flowchart.bmp

the circuit in Proteus is attached below
View attachment homework.BMP

I look for your help as soon as possible
 

Your schematic is wrong!!!

In the switches... look the example... disregard the crystal if using internal oscillator!!!

View attachment Semaforo.BMP

Asm tutorial : **broken link removed**

This is basic code: Follow your flow chart!!!
Code:
;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F877A. This file contains the basic code              *
;   building blocks to build upon.                                    *  
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F877A.INC                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************

	list		p=16f877A		; list directive to define processor
	#include	<p16f877A.inc>	; processor specific variable definitions
	
#DEFINE		BANK0	BCF		STATUS,RP0
#DEFINE		BANK1	BSF		STATUS,RP0
	
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _RC_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
	
	ERRORLEVEL  -302			; Eliminate bank warning
	
; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;***** VARIABLE DEFINITIONS
	CBLOCK	0x20
		W_TEMP					; variable used for context saving
		STATUS_TEMP				; variable used for context saving
		PCLATH_TEMP				; variable used for context saving	
		TEMP1					; For delay routine
		TEMP2					; For delay routine
		TEMP3					; For delay routine
		TEMP4					; For delay routine
	ENDC
;**********************************************************************
	ORG     0x000				; processor reset vector

	NOP			  				; nop required for icd
  	GOTO    MAIN				; go to beginning of program

	ORG     0x004				; interrupt vector location

	MOVWF   W_TEMP				; save off current W register contents
	MOVF	STATUS,W			; move status register into W register
	MOVWF	STATUS_TEMP			; save off contents of STATUS register
	MOVF	PCLATH,W			; move pclath register into w register
	MOVWF	PCLATH_TEMP			; save off contents of PCLATH register

; isr code can go here or be located as a call subroutine elsewhere

	MOVF	PCLATH_TEMP,W		; retrieve copy of PCLATH register
	MOVWF	PCLATH				; restore pre-isr PCLATH register contents
	MOVF    STATUS_TEMP,W		; retrieve copy of STATUS register
	MOVWF	STATUS				; restore pre-isr STATUS register contents
	SWAPF   W_TEMP,F
	SWAPF   W_TEMP,W			; restore pre-isr W register contents
	RETFIE						; return from interrupt
;**********************************************************************
MAIN
	BANK0
	CLRF	PORTA
	CLRF	PORTB
	CLRF	PORTC
	CLRF	PORTD
	CLRF	PORTE
	BANK1
	;		  76543210	bit control
	MOVLW	B'00000000'			; All Output
	MOVWF	TRISA
	;		  76543210	
	MOVLW	B'00000011'			; B0 and B1 as Input the rest is Output
	MOVWF	TRISB
	;		  76543210	
	MOVLW	B'00000000'			; All Output
	MOVWF	TRISC
	;		  76543210	
	MOVLW	B'00000000'			; All Output
	MOVWF	TRISD
	;		  76543210	
	MOVLW	B'00000000'			; All Output
	MOVWF	TRISE
	;		  76543210	
	MOVLW	B'10000000'			; PORTB pull-ups are disabled
	MOVWF	INTCON
	BANK0	
;**********************************************************************
LOOPING
	;		  76543210	bit control
	MOVLW	B'00000000'			; 0 in BCD 
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000001'			; 1 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000010'			; 2 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000011'			; 3 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000100'			; 4 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000101'			; 5 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000110'			; 6 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000111'			; 7 in BCD
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00001000'			; 8 in BCD 
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00001001'			; 9 in BCD 
	MOVWF	PORTD
	CALL	DELAY100M			; Call 100m delay
	MOVLW	B'00000000'			; 0 in BCD 
	MOVWF	PORTD

	;		  76543210	bit control
	MOVLW	B'00000001'
	MOVWF	PORTA
	MOVLW	B'10000000'
	MOVWF	PORTB
	CALL	DELAY1S				; Call 1S delay
	MOVLW	B'00000010'
	MOVWF	PORTA
	MOVLW	B'01000000'
	MOVWF	PORTB
	CALL	DELAY1S				; Call 1S delay
	MOVLW	B'00000100'
	MOVWF	PORTA
	MOVLW	B'00100000'
	MOVWF	PORTB
	CALL	DELAY1S				; Call 1S delay
	MOVLW	B'00000000'
	MOVWF	PORTA
	MOVLW	B'00000000'
	MOVWF	PORTB
	CALL	DELAY1S				; Call 1S delay

	BTFSC	PORTB,0				; Read Key in PORTB RB0
	CALL	FUNCTIONA	
	CALL	FUNCTIONB

	BTFSC	PORTB,1				; Read Key in PORTB RB1
	CALL	FUNCTIONC	
	CALL	FUNCTIOND

	GOTO LOOPING
;**********************************************************************
FUNCTIONA						; Make here your code

	RETURN
;**********************************************************************
FUNCTIONB						; Make here your code

	RETURN
;**********************************************************************
FUNCTIONC						; Make here your code

	RETURN
;**********************************************************************
FUNCTIOND						; Make here your code

	RETURN
;**********************************************************************
; Delay 100 mili routine
DELAY100M
	MOVLW	0x1E
	MOVWF	TEMP1
	MOVLW	0x4F
	MOVWF	TEMP2
DELAYLOOP
	DECFSZ	TEMP1, F
	GOTO	$+2
	DECFSZ	TEMP2, F
	GOTO	DELAYLOOP
	GOTO	$+1
	NOP
	RETURN
;**********************************************************************
DELAY1S
	MOVLW	0X07
	MOVWF	TEMP1
	MOVLW	0X2F
	MOVWF	TEMP2
	MOVLW	0X03
	MOVWF	TEMP3
DELAY1SLOOP
	DECFSZ	TEMP1, F
	GOTO	$+2
	DECFSZ	TEMP2, F
	GOTO	$+2
	DECFSZ	TEMP3, F
	GOTO	DELAY1SLOOP
	GOTO	$+1
	GOTO	$+1
	GOTO	$+1
	RETURN	
;**********************************************************************
	END                       ; directive 'end of program'
;**********************************************************************
 

Attachments

  • Semaforo High.BMP
    4 MB · Views: 195
Last edited:

Dear
the circuit is given by the instructor, although i wrote the code in MikroC and it's running good, can i convert my code to assembly and use MBLAB ?!!

Here is the code
Code:
int i;
char DSP_code1[10]= {0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};
char DSP_code2[6] = {0x05,0x04,0x03,0x02,0x01,0x00};
void delay1 ()
{
 for(i=0; i<10; i++)
 {
    PORTD = DSP_code1[i];
    delay_ms(1500);
 }
}

void delay2 ()
{
 for(i=0; i<5; i++)
 {
    PORTD = DSP_code2[i];
    delay_ms(1000);
 }
}


void main()
{
TRISA = 0x00;
TRISB = 0x00;
TRISD = 0x00;

while (1)
{
 porta = 0x01;
 portb = 0x80;
 delay1();
 portb = 0xC0;
 delay2();
 porta = 0x04;
 portb = 0x20;
 delay1();
 porta = 0x06;
 delay2();
}
}

- - - Updated - - -

Dear
the circuit is given by the instructor, although i wrote the code in MikroC and it's running good, can i convert my code to assembly and use MBLAB ?!!

Here is the code
Code:
int i;
char DSP_code1[10]= {0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};
char DSP_code2[6] = {0x05,0x04,0x03,0x02,0x01,0x00};
void delay1 ()
{
 for(i=0; i<10; i++)
 {
    PORTD = DSP_code1[i];
    delay_ms(1500);
 }
}

void delay2 ()
{
 for(i=0; i<5; i++)
 {
    PORTD = DSP_code2[i];
    delay_ms(1000);
 }
}


void main()
{
TRISA = 0x00;
TRISB = 0x00;
TRISD = 0x00;

while (1)
{
 porta = 0x01;
 portb = 0x80;
 delay1();
 portb = 0xC0;
 delay2();
 porta = 0x04;
 portb = 0x20;
 delay1();
 porta = 0x06;
 delay2();
}
}
 

You can use the template for asm or use the .LST and .ASM (compiled by mikroC) file to make the .ASM for MPASM!!!

In folder C:\Program Files (x86)\Microchip\MPASM Suite\Template\Code\ or C:\Program Files\Microchip\MPASM Suite\Template\Code\

Search for 16F877ATEMP.ASM
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top