Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F877 - Prescaler - Newbie

Status
Not open for further replies.

mpride63

New Member
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.
 
Hi,

I just glanced overyour code (its late) and it looks like you're already using the prescaler, at 1/256:

MOVLW B'00100111'
MOVWF OPTION_REG

For external clock source (change on T0CK1 pin) bit 5 sohuld be set, as it is, and the last 3 bits determine the prescaler value....000 would be 1:1, but 111 (like yours is 1:256.

Is this a crystal? because to use a crystal osc you should use TMR1.

Do you have the datasheet?
https://www.electro-tech-online.com/custompdfs/2006/11/39582b-2.pdf

It tells you exactly what registers control what.

One tip, if you're using the MSPASM enviroment, instead of setting/clearing the bank select bits, every time you want to be able to read/write to a peripheral register (for timers, PWM, ADC etc..) use MPASM's built in macro 'banksel <nameofregister>'.

The number of times I've spent debugging code all because I didn't set the bank select bits right, for the right register...and using the macro could be considered 'lazy', but its helpful.

As for having trouble getting portA to be an input.... have you set the appropriate 'trisA' bits? ie: 11111111 for all inputs (or just 0xFF, if the radix is hex). It might be that there is a peripheral the PIC has that is 'on by default' that uses some pins of PORTA, so regardless of what you write to the tris reg, they'll be used by the peripheral. For example, find the register to control the ADC in the 877A, and write the appropriate byte to it to turn it off.

The part starting ';Initialize and Set Ports Input and Output State'
All that code has ; in front of it, so it is ignored by the compiler as annotation, was this intentional? like for debugging?

Anyway, when you're starting, its good to get used to things. So start by trying to use all the ports effectively, by configuring the 'tris' registers, and turning off any peripherals that appear to be 'on' at start up (read the datasheet). Then, concentrate on configuring any peripherals you wish to use. Then start on any 'functional code' you wish.

My two cents,

Blueteeth.
 
First, let me say thanks for the quick and helpful reply.

Let me try to go through the highlites of the reply:

Is this a crystal? because to use a crystal osc you should use TMR1.

Yes it's a crystal. I can try to use TMR1 but I'm curious why TMR0 won't work?

use MPASM's built in macro

Thanks for the tip. I think I'll look to printout all the macros and begin using them.

It might be that there is a peripheral the PIC has that is 'on by default' that uses some pins of PORTA

I'll have to look into that.

The part starting ';Initialize and Set Ports Input and Output State'

You are correct. Just me playing and trying various things.

I will probably try to make the changes to the code tomorrow and will post again to let you know what I end up with.

Thanks once again.
 
Hi again,

Sorry for such a disorganised reply... probably should have answered each question specifically, but I generally just spurt out idea's in a random order :D

mpride63 said:
First, let me say thanks for the quick and helpful reply.

You're welcome.

mpride63 said:
Is this a crystal? because to use a crystal osc you should use TMR1.

Yes it's a crystal. I can try to use TMR1 but I'm curious why TMR0 won't work?

Hmm, well, the external input for timer0 only has one pin, and this pin expects a nice square wave (well, not a 50/50 duty cycle, but a digital input), I don't think it has a 'schmitt trigger' input to clean up noisy signals...it really just requires a nice TTL input. In order to use an external crystal (like the PIC's oscillator) with just the Xtal and two caps, two pins are required to get the crystal oscillating, I'm not too good at explaining this :/

Timer1 on the other hand, can be configured to use its two pins for an external Xtal oscillator, there is a TTL inverter connected between these two pins, inside the PIC, which allows a crystal to be connected across it, it starts oscillating on powerup. Page 60 on the datasheet shows the diagram for timer1's operation and can explain it better than I ever could.

See.. https://www.electro-tech-online.com/custompdfs/2006/11/00580c.pdf

Interestingly....you CAN use TMR0 for an external oscillator..like those oscillator modules.
Example: **broken link removed**

All these are really is a crystal and driving circuitry, so they oscillate at their frequency, and provide a TTL output. This could be connected to the T0CK1 pin (for TMR0) and there you go. But, if you just have a crystal, and would like to user TMR0 instead of TMR1, you would need to have a driver to get the crystal to oscillate, as the PIC T0CK1 pin, is not designed to do this itself.

Some exampes of a small external circuit:
**broken link removed**
**broken link removed**

mpride63 said:
It might be that there is a peripheral the PIC has that is 'on by default' that uses some pins of PORTA

I'll have to look into that.

I vaguely remember having trouble with the 628A because of this, it had the comparator module on, on powerup. But of course, Once you've written a startup routine, then you can use that as your default 'startup' for that PIC. I don't think I've written original code to sort out the PORT's in over a year...just copy/paste in some default code, and modify it for my needs (I'm working on a simple code generator, in software, with tick boxes etc.. for this, just to speed things up a bit). Although copying/pasting old code has it own problems, because any mistakes in the original are copied...anyway, thats how I do it.

Let me know how you get on, I swear, even with the most complicated programs I've written for PIC's have been hindered by silly mistakes, like banking, ports configured wrong, missing out 'return' after subroutines etc.. Some well written code 'snippets' will save you a lot of time in the future :D

Blueteeth.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top