1. 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.
    Dismiss Notice

characteristics and apps: PIC16F870

Discussion in 'Microcontrollers' started by sardineta, Oct 1, 2004.

  1. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    This is what I want:

    initializate interrupt for RB0/INT
    so it will be always runing, when and event is occurred a counterA will be incremented

    intializate ADC, I can start/stop the ADC when I want

    Initializate timer0
    it will run forever, and generates an interrupt every 20ms

    if by the ADC I detect 1.25V = (ADRESH=0, ADRESL=256)
    and if this event occurs for an equal interval of 300ms then I should turn on a led and increment counterB. then after 1 minute I should turn off the led

    if againf by the ADC I detect 1.25V, and it persists for 300mS I should turn on a led, after one minute I should stop the led and increment counterB.


    but, if before the second event there haven't been any other event for about 30 minutes then I should clear counterB

    how i can gent the 30 minutes, without taking processing time as delays do
    with timer1, this is a 16 bit register.

    also I want to know if ADC can run without taking processing as delays
     
  2. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    If you're already using timer0 to generate interrupts every 20mS, use that same clock - simply increment, or decrement, registers in order to get the time you require. For a 20mS interrupt the first obvious counter is from (or to) 50, to give a count in seconds, from that you count again, from (or to) 60, to give minutes - and so on as long as you need!.
     
  3. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    Ok, That is a good idea, but it cause me pain.

    :?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos

    ADC Vs RB0

    what happens when i am waiting the ADC to finish and before it occurs and interrupt from RB0 occurs, does the ADC still sample the signal or it stops and then will start again the sampling or it will go through the code.
     
  6. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Re: ADC Vs RB0

    The ADC operates seperately from the processor, so it will continue to sample while the interrupt routine is active. When the interrupt routine returns it will find the flag set if the ADC has finished - assuming you're using polling to check for the ADC finishing?.
     
  7. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    thanks a lot!
    what about if I am not polling the ADC, I mean I will enable this periphereal interrupt and If ADC finish while RB0 interrupt was attended or any other interrupt is generated.


    what happens?

    I will use the followinf interrupts
    TIMER0 every 20ms
    RB0/INT, external event
    ADC
     
  8. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    You should arrange your program to minimise the number of simultaneous interrupts - when an interrupt is called it disables further interrupts, and your interrupt code should enable then again as it exits, when any pending interrupts will be serviced.

    I don't claim to be an expert on PIC interrupts, I only use them occasionally - mostly you can avoid using them - from your description so far it looks like you probably only require the 20mS timer interrupt. Most interrupt intensive code you see consists of sitting in endless loops waiting for an interrupt to occur, you may as well carry out polling during this loop instead.
     
  9. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    ADC

    I have heard that the input impedance to the ADC from the microcontroller should be 2.5K.

    what about if I directly connect a potentiometer to handle a voltage level to the ADC input.

    what about the input impedance?

    this effect is saw when using opamps? or when I should take care of it?

    c u
     
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Re: ADC

    The main problem is the speed of charging the internal sample and hold capacitor, you may have to wait a substanial time between samples, particularly when switching channels. Although it's been suggested that accuracy can suffer slightly as well - but I've not checked this, from the suggestion accuracy should still be plenty higher than your support components (so I don't see it as a problem!).

    If you feed the A2D input from an opamp you won't see the effect, you should do this is you want maximum speed!.
     
  11. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    I have 2Kx14 of flash memory (program memory) in the 16F870 uC

    I have already compiled my asm code and this process generated a hex file of 2657 bytes.

    Does this file fit in the PROGRAM MEMORY?

    Another question:
    Every variable I need, it should be initializad to zero by software?.

    --------------------------
    example equ 0x20
    ....

    movlw 0x00
    movwf example
    --------------------------

    or on power on data memory all bytes are zero.
    I would like to know it because if ram is initialized to 0's on power on my code redeuce because I do not do it by software, but it still is above 2400 bytes.


    Thanks in advanced
     
  12. Exo

    Exo Active Member

    Joined:
    Sep 18, 2003
    Messages:
    1,953
    Likes:
    1
    Location:
    Belgium
    The hex file contains the data as ascii indormation and it also carries address information. This makes the hex file a LOT! larger then the actual code size.

    if you want to know how much memory is required you should let the compiler generate a list file, it tells you how much memory is required.
    You can also see it when you load the file into your programmer.

    It's always best to initialize any variables you use.
    To make your code smaller you can use indirect adressing to clear a whole block of memory in a small loop.
     
  13. williB

    williB New Member

    Joined:
    Aug 30, 2004
    Messages:
    2,217
    Likes:
    3
    Location:
    New Jersey
    it should fit because 1/4 of the hex file or assembled program are zeros.. this equals 1992 14 bit bytes.. so it should fit..
     
  14. Exo

    Exo Active Member

    Joined:
    Sep 18, 2003
    Messages:
    1,953
    Likes:
    1
    Location:
    Belgium
    How many zero's are in it doesn't matter
    it's a intel hex file (do a google search). The data is stored as ascii wich makes it twice as large as it actually is. It also holds adress information for every 16 bytes of data wich doesn't get transferred.
     
  15. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    thanks a lot.
    I will change the direct initialization mode by indirect mode
    and the file size it makes feel light. (ligerito)
     
  16. williB

    williB New Member

    Joined:
    Aug 30, 2004
    Messages:
    2,217
    Likes:
    3
    Location:
    New Jersey
    i am well aware of the extra digits in a hex file..
    but i was just looking at a 70 byte hex file dos file size...and it had 66 hex digits in it plus two coln's =68 bytes plus the carrage reuurn & line feed , makes 70 bytes.. but it wasnt 132 bytes long ..go figure...

    anyway i agree there are 6 bytes per line that dont get programmed into the part..counting the coln...

    what i was talking about was 2 bytes of hex file data , can fit into one 14 bit program memory location...so the 2048 program memory of the 16F870 could hold a hex file at least 4096 bytes long ...
     
  17. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    Thanks a lot for your comments.
    I really appreciate that.

    I realized that I have a ommited a register. This is the Configuration Register, some parameters can be very easy deduced but others I really do not know:

    I have it in my _CONFIG

    _CONFIG (MSB) NC,NC,11 0111 x011 1010 (LSB)

    bit 13&12 =4&5 = 11, code protection off
    bit 11 = 1, in-circuit debugger disable
    bit 10 unimplemented

    bit 9 WRT = 1, unprotected program memory may be written to
    WHAT YOU RECOMMEND ME in bit 9

    bit 8 CPD = 1 code protection OFF

    bit 7 LVP = X I do not know what's in here
    WHAT YOU RECOMMEND ME in bit 7

    bit 6 BOREN = 0 disable
    bit 3 PWRTEN = 1 disable
    bit 2 WDTEN = 0 disable
    bit 1 y 0 Fosc1 y 2 = 10 HS oscillator



    I will use a burner called from "solaris digital" model S-103
    so I think some control bits depends on it, isn't it?
    what you recommend me to do with the configuration words

    thanks in advanced
     
  18. williB

    williB New Member

    Joined:
    Aug 30, 2004
    Messages:
    2,217
    Likes:
    3
    Location:
    New Jersey
    bit 7 LVP: Low Voltage In-Circuit Serial Programming Enable bit
    1 = RB3/PGM pin has PGM function, low voltage programming enabled
    0 = RB3 is digital I/O, HV on MCLR must be used for programming..
    low voltage programming is with the on chip serial port .. i assume that u are using MCLR to program the part so bit 7 is a zero
     
  19. williB

    williB New Member

    Joined:
    Aug 30, 2004
    Messages:
    2,217
    Likes:
    3
    Location:
    New Jersey
    i am pretty sure that bit 9 has to do with on chip serial programming.. also.. but i am not sure 100 % ...anyone else..??

    bit 9 WRT: FLASH Program Memory Write Enable
    1 = Unprotected program memory may be written to by EECON control
    0 = Unprotected program memory may not be written to by EECON control
     
  20. sardineta

    sardineta New Member

    Joined:
    Sep 27, 2004
    Messages:
    131
    Likes:
    0
    Location:
    Cuernavaca Morelos
    the correct initialization of a program should be


    org 0x00
    goto MAIN ;main program

    org 0x04
    goto ISR ;interruption service routine

    MAIN ;main program begin here
    blablabla

    ISR ;ISR begin here
    blablabla
    RETFIE

    END


    I have seen that some people use org 0x05, does anyone know why?
    I have seen it from Nigel's programs and I really do not undestand why ORG 0x00 is sometimes 0x20 for example.
    is it really important, for the ISR?

    I have my program finished, but when I simulate it in the MPLAB, the TMR0, is not reloaded, does I need to reload it at the end of the ISR before exit? before bcf INTCON T0IF
     
  21. Exo

    Exo Active Member

    Joined:
    Sep 18, 2003
    Messages:
    1,953
    Likes:
    1
    Location:
    Belgium
    Where have you seen that? i've never come across code wich used 0x05 as the origin for the ISR.

    Take a better look, it's the cblock that starts at 0x20 in Nigel's examples. The cblock is where variables in RAM are declared.
     

Share This Page