• AVR Freaks

Hot!Trouble using PIC16F18346 TMR0 to read IR signal

Author
Gesumar72
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/06/29 23:24:42
  • Location: 0
  • Status: offline
2019/07/06 17:15:22 (permalink)
0

Trouble using PIC16F18346 TMR0 to read IR signal

Hi, I am new to this community and PIC.
Right now, I am working on a project where the PIC recognizes the Infra^Red signal and moves differently depending on which signal is received.
I have found out that the receiver module is able to receive and signal something out, but I just cannot make the program to match its output.
I googled everything using copy paste to build up my program, so I am thinking that basic lack of understanding PIC is missing.
Could someone tell me what I am doing wrong with my program?( I am guessing I am not using TMR0 correctly)
The pic I am using is PIC16F18346 and I want to compare IR input ( module output) with the readied array of numbers.
TMR0 is used to measure the length of IR signal.
 
 
#include <xc.h>

// CONFIG1
#pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LOW // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

unsigned char i;
#define IR RA1
#define N 8

const unsigned char sig[N] = {
0,0,0,0,1,0,0,0
};

void main(void) {
unsigned char i,j,m;
unsigned char t[9],s[8];
TRISB = 0b00001000;
PORTB = 0;
PORTA = 0;
TRISA = 0b00000000;
OPTION = 0b10000100;
#define _XTAL_FREQ 1000000
T0CON0 = 0b00000000;
T0CON1 = 0b01100101;
T0EN = 1 ;
    TMR0H = 0xFF;
    TMR0L = 0x00;



while(1){
    LATA2 = 1;

    while(RA3){
    }
    LATA2 = 0;

TMR0 = 0;
while(TMR0 < 100){
if(IR) TMR0 = 0;
}
while(IR == 0);
while(IR);

for(i = 0; i < 9; i++){
while(IR == 0);
TMR0 = 0;
while(IR && (TMR0 < 100));
t[i] = TMR0;
if(t[i] >= 80) break;
}

m = 0;
for(i = 0; i < N; i++){
if(t[i] < 35) s[i] = 0;
else s[i] = 1;
if(s[i] = sig[i]) m = m + 1;
}

if(m = N) LATA0 = 1;
else LATA2 = 1;

for(j = 0; j < 25; j++){
TMR0 = 0;
while(TMR0 < 255);
}
LATA0 = 0;
LATA2 = 0;
}
}


Given my poor understanding of TMR0, I also tried below program to see if TMR0 is working.
but LATA2 never lit up.
I would love to get any advice from anyone.
Thanks very much,
#include <xc.h>

// CONFIG1
#pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LOW // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

 
#define _XTAL_FREQ 1000000
unsigned char i;
#define IR RA1
#define N 8
const unsigned char sig[N] = {
0,0,0,0,1,0,0,0
};





void main(void) {
unsigned char i,j,m;
unsigned char t[9],s[8];
PORTA = 0;

    ANSELA = 0b00000000;
    TRISA = 0b00001010;

T0CON0 = 0b00000000;
T0CON1 = 0b01100101;
TMR0H = 0xFF;
TMR0L = 0x00;
T0CON0bits.T0EN = 1;


while(1){
    TMR0 = 0;
    if(TMR0 > 100){
LATA2 = 1;
}


    __delay_ms(950);
     while(RA3){
    }
         LATA0 = 1;
         
__delay_ms(950);
              while(RA3){
    }
LATA0 = 0;

    }
    return;

}

 
 
#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 22715
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/08 16:00:48 (permalink)
    +1 (1)
    You never mention where the IR signal is connected. I assume it is on RA3, as you test that pin, but you wrote 0b00000000 to TRISA, which makes ALL the PORTA pins outputs!
     
    In your second code block, you do this:
        TMR0 = 0;
        if(TMR0 > 100){

    How do you expect TMR0 to ever be more than 100 when you only just wrote zero to it?
     
     
    For your own sanity (and that of people trying to help you) can I make two suggestions?
    [1] Avoid single letter variable names like the plague. Give your variables meaningful names.
    [2] Comment your code, saying what you expect it to do. Don't expect snippets of code borrowed form the Internet to "just work".
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    pcbbc
    Super Member
    • Total Posts : 1101
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/08 21:28:14 (permalink)
    +1 (1)
    [3] Indent your code consistently.

    Is your IR signal PWM encoded? You might be a lot better off using the capture modules described in chapter 29. That way the hardware will measure the time between successive pulses and just tell you when it has detected one, and what the timer value was at that instant.

    1. Connect the filtered IR signal form the sensor to a CCPx pin,
    2. Configure the capture module to user timer0, and capture every rising (or falling if you require/prefer) edge with prescale of 1,
    3. Wait for CCPxIF == 1 to indicate a capture. Reset the interrupt flag: CCPxIF = 0,
    4. Read the captured timer value from CCPRxH:CCPRxL,
    5. Subtract the previous captured value to get the elapsed time (interval between pulses),
    6. Repeat until you have captured the entire waveform.
    #3
    Gesumar72
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/29 23:24:42
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/09 08:11:28 (permalink)
    0
    Thank you ric.
    Sorry for my lack of explanation, RA3 is intended for IR input, which I had completely overlooked...
    And about TMR0, I thought that TMR0 would count up automatically after several steps of waiting which seemed wrong.
    This was my first time posting, and my first challenge, so I could not consider enough of how a proper post would be like.
    But thank you very much for your suggestions.
    I will try to edit the post around tomorrow hoping that there any chances of suggestions and advice left for me.
    #4
    Gesumar72
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/29 23:24:42
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/09 08:29:11 (permalink)
    0
    Thank you pcbbc.
    I will try editing with more indent.
    As of PWM, I am guessing that the IR signal is PWM modulated since I am picking up the signal from commercially available NEC format remote controller.
    By capture modules did you mean "CAPTURE/COMPARE/PWM MODULES"?
    It might take a while, but I will try building up a code in an order you have mentioned.
     
     
    #5
    pcbbc
    Super Member
    • Total Posts : 1101
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/09 09:17:51 (permalink)
    +1 (1)
    Gesumar72As of PWM, I am guessing that the IR signal is PWM modulated since I am picking up the signal from commercially available NEC format remote controller.

    It's not really PWM in the strictest sense, although you often see it called that (or sometimes PPM, which it isn't either).
    Usually I would expect PWM to have a fixed period, but varying duty cycle:
         _
    0 = | |___
         ___
    1 = |   |_


    Whereas with the NEC protocol the "bits" are conveyed by varying the length of the gaps:
         _
    0 = | |__
         _
    1 = | |______

    Pulse Distance Coding seems to be what Vishay call it.
     
    By capture modules did you mean "CAPTURE/COMPARE/PWM MODULES"?

    Yes.  These allow you to accurately measure the time between two pulses using hardware.
    #6
    ric
    Super Member
    • Total Posts : 22715
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/09 13:25:58 (permalink)
    +1 (1)
    Gesumar72
    And about TMR0, I thought that TMR0 would count up automatically after several steps of waiting which seemed wrong.

    You skipped the "several steps of waiting" ...
    Maybe you intended to zero TMR0 before the start of your loop, not inside it.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    Gesumar72
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/29 23:24:42
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/10 08:01:22 (permalink)
    0
    Thanks pcbbc.
    I really appreciate the additional information, with also a very easy to understand schematics.
    Reviewing my programs and also checking on rics advice, I feel there is a massive need of revision on my programs.
    So I will try the revision program with also CCP variant. ( this takes time)
    If I get stuck again, I might question again with either current or a new post.
    #8
    Gesumar72
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/29 23:24:42
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/10 08:06:59 (permalink)
    0
    Thanks ric.
    Reviewing my program and reading your advice, I found more places I need to revise.
    So I will make a big revision on my program and give it a try once more.
     
    #9
    PStechPaul
    Super Member
    • Total Posts : 2296
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/10 14:36:03 (permalink)

     
    #10
    Gesumar72
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/29 23:24:42
    • Location: 0
    • Status: offline
    Re: Trouble using PIC16F18346 TMR0 to read IR signal 2019/07/12 07:51:59 (permalink)
    0
    Thank you PStechPaul!
    I will learn.
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5