Hi.
I decided a couple of months ago I wanted to learn about microcontrollers (read… extreme newbie). I have written a small amount of code in assembly that I hope to use as a basis to complete my project. However, I’m having all kinds of problems implementing a prescaler. I’m trying to use TMR0 (should I use another timer?) and an external 32KHz oscillator. I want to use a prescaler of 1/256. I have figured out how to configure delays etc. but my life would be a lot easier if I could understand how to change my code to take advantage of the prescaler.
I’m using a PIC16F877 and running my simulations with MPLAB SIM. I’m not sure how to do this and would greatly appreciate any help. Yes, I’ve read other posts on the subject but still can’t get it through my thick skull.
--------------------------------------
TMR0 EQU 1
OPTION_REG EQU 81H
PORTA EQU 5
PORTB EQU 6
PORTC EQU 7
PORTD EQU 8
TRISA EQU 5
TRISB EQU 6
TRISC EQU 7
TRISD EQU 8
STATUS EQU 3
ZEROBIT EQU 2
CARRY EQU 0
EEADR EQU 0DH
EEDATA EQU 0CH
EECON1 EQU 0CH
EECON2 EQU 0DH
RD EQU 0
WR EQU 1
WREN EQU 2
COUNTER_A EQU 20H
COUNTER_B EQU 21H
;********************************************************
LIST P=16F877
ORG 0 ;the start address in memory is 0
;*********************************************************
;Initialize and Set Ports Input and Output State
;MOVLW B'00000111'
;MOVWF TMR0
;BSF STATUS,5
;MOVLW B'00000000'
;MOVWF TRISB
;MOVLW B'00000000'
;MOVWF TRISC
;MOVLW B'00000000'
;MOVWF TRISD
;BCF STATUS,5
;**********************************************************
;Program Setup
BSF STATUS,5
MOVLW B'00100111'
MOVWF OPTION_REG
BCF STATUS,5
BEGIN CLRF COUNTER_A
MOVLW B'11111111'
MOVWF COUNTER_A
CLRF COUNTER_B
MOVLW B'00100101'
MOVWF COUNTER_B
;*********************************************************
;Monitor Inputs
MONITOR BTFSC PORTC,0 ;Test bit 0 in file PORTC and skip next instrucion if clear (low)
GOTO MONITOR ;Loopback to Monitor if bit 0 in file PORTC is set (high) -
;*********************************************************
;Counter Definition
COUNTER_DEF DECFSZ COUNTER_A
GOTO ALARM_ON
DECFSZ COUNTER_B
GOTO ALARM_ON
GOTO ALERTS_ON
GOTO BEGIN
;*********************************************************
;Create Alerts
ALARM_ON BTFSC PORTC,0 ;Test bit 0 in file PORTC
GOTO ALARM_OFF
GOTO COUNTER_DEF
ALERTS_ON BSF PORTB,0 ;Turn on the Red Status LED
BSF PORTD,0 ;Turn on Buzzer
;Send ON Command
GOTO BEGIN
ALARM_OFF BCF PORTB,0 ;Turn off the LED
BCF PORTD,0 ;Turn off Buzzer
;Send OFF Command
GOTO BEGIN ;Loopback to Monitor
END
;Figure out how to control the clock.
;Find out why I have trouble using PORTA as an input.
;Figure out exactly what needs to be initialized.
;Configure the program to look at ports and not just the individual pins.
I decided a couple of months ago I wanted to learn about microcontrollers (read… extreme newbie). I have written a small amount of code in assembly that I hope to use as a basis to complete my project. However, I’m having all kinds of problems implementing a prescaler. I’m trying to use TMR0 (should I use another timer?) and an external 32KHz oscillator. I want to use a prescaler of 1/256. I have figured out how to configure delays etc. but my life would be a lot easier if I could understand how to change my code to take advantage of the prescaler.
I’m using a PIC16F877 and running my simulations with MPLAB SIM. I’m not sure how to do this and would greatly appreciate any help. Yes, I’ve read other posts on the subject but still can’t get it through my thick skull.
--------------------------------------
TMR0 EQU 1
OPTION_REG EQU 81H
PORTA EQU 5
PORTB EQU 6
PORTC EQU 7
PORTD EQU 8
TRISA EQU 5
TRISB EQU 6
TRISC EQU 7
TRISD EQU 8
STATUS EQU 3
ZEROBIT EQU 2
CARRY EQU 0
EEADR EQU 0DH
EEDATA EQU 0CH
EECON1 EQU 0CH
EECON2 EQU 0DH
RD EQU 0
WR EQU 1
WREN EQU 2
COUNTER_A EQU 20H
COUNTER_B EQU 21H
;********************************************************
LIST P=16F877
ORG 0 ;the start address in memory is 0
;*********************************************************
;Initialize and Set Ports Input and Output State
;MOVLW B'00000111'
;MOVWF TMR0
;BSF STATUS,5
;MOVLW B'00000000'
;MOVWF TRISB
;MOVLW B'00000000'
;MOVWF TRISC
;MOVLW B'00000000'
;MOVWF TRISD
;BCF STATUS,5
;**********************************************************
;Program Setup
BSF STATUS,5
MOVLW B'00100111'
MOVWF OPTION_REG
BCF STATUS,5
BEGIN CLRF COUNTER_A
MOVLW B'11111111'
MOVWF COUNTER_A
CLRF COUNTER_B
MOVLW B'00100101'
MOVWF COUNTER_B
;*********************************************************
;Monitor Inputs
MONITOR BTFSC PORTC,0 ;Test bit 0 in file PORTC and skip next instrucion if clear (low)
GOTO MONITOR ;Loopback to Monitor if bit 0 in file PORTC is set (high) -
;*********************************************************
;Counter Definition
COUNTER_DEF DECFSZ COUNTER_A
GOTO ALARM_ON
DECFSZ COUNTER_B
GOTO ALARM_ON
GOTO ALERTS_ON
GOTO BEGIN
;*********************************************************
;Create Alerts
ALARM_ON BTFSC PORTC,0 ;Test bit 0 in file PORTC
GOTO ALARM_OFF
GOTO COUNTER_DEF
ALERTS_ON BSF PORTB,0 ;Turn on the Red Status LED
BSF PORTD,0 ;Turn on Buzzer
;Send ON Command
GOTO BEGIN
ALARM_OFF BCF PORTB,0 ;Turn off the LED
BCF PORTD,0 ;Turn off Buzzer
;Send OFF Command
GOTO BEGIN ;Loopback to Monitor
END
;Figure out how to control the clock.
;Find out why I have trouble using PORTA as an input.
;Figure out exactly what needs to be initialized.
;Configure the program to look at ports and not just the individual pins.