Serial on 16f1509

Discussion in 'Programmer's Corner' started by FroceMaster, Apr 26, 2013.

  1. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Hi
    Have following code.
    and can't seem to get anything back from my chip.
    When pressed on a button, it should write a number.
    Use Pickit2 as UART Tool, and when connecting RX to TX, i get Things back.
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include "lcd.h"
    6. #include <string.h>
    7. __CONFIG (CLKOUTEN_OFF & FCMEN_ON & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSC);//XT
    8. __CONFIG (LVP_ON & LPBOR_OFF & BOREN_ON & STVREN_ON & WRT_OFF);
    9. #define _XTAL_FREQ 4000000
    10. #define button RC1
    11. void usrt_init()
    12. {
    13.     TRISB5=1; // in
    14.  TRISC1=1; // in
    15.  TRISB7=0; // out
    16.     ANSELB=0; // analog off.
    17.  
    18.     //TXSTA    //CHECK THE DATA SHEET FOR TXSTA
    19.     CSRC=0; //Clock Source Select bit not used in async.
    20.     TX9=0; // 9-bit Transmit Enable bit
    21.     TXEN=1; // Transmit Enable bit(1)
    22.     SYNC=0; //1 = Synchronous mode 0 = Asynchronous mode
    23.     SENDB=0; //Send Break Character bit 1 = Send Sync Break on next transmission (cleared by hardware upon completion)
    24.     //0 = Sync Break transmission completed
    25.     BRGH=0; //High Baud Rate Select bit 0= low
    26.     TRMT=1; //Transmit Shift Register Status bit 1 = TSR empty 0 = TSR full
    27.     TX9D=0; //Ninth bit of Transmit Data
    28.  
    29.  
    30.     //RCSTA    //SEE THE DATA SHEET FOR RCSTA
    31.     SPEN=1; //Serial Port Enable bit
    32.     RX9=0;  // 8bit
    33.     SREN=0; // Single Receive Enable bit
    34.     CREN=1; //1 = Enables receiver
    35.     ADDEN=0; //Not Used.
    36.     FERR=0; //Framing Error bit
    37.     OERR=0; // Overrun Error bit
    38.     RX9D=0; // Ninth bit of Received Data
    39.  
    40.  
    41.     BRGH=0;            //  low baud rate  
    42.     SPBRGL=0b11001111;      //baud rate 300
    43.     SPBRGH=0b00000000;
    44.  
    45. }//
    46. main()
    47. {    
    48.     usrt_init();
    49.     while(1); //loop
    50.      {
    51.       if (!button) // if pressed the button.
    52.       {
    53.        TXEN=1;
    54.         TXREG = 0b11111100; //sends a number
    55.             while(!TRMT); // until empty.
    56.       }
    57.      }
    58. }  
    59.  
    Only button is held high by 10 k, and is low when pressed.
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Without a schematic, this will be a best-guess scenario...

    Try sending 0x30, this well send "0", the character you are trying to send may not show up.


    When you say you are connecting RX to TX, I'm assuming you mean on the pickit, to verify the UART on it is working, is this correct? The statement is a bit ambiguous and I want to be concise.

    Oop, never mind, I found it! A semicolon after your while (1) says the body is empty, your code will never reach transmit! Take out that semicolon...
     
  3. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Thanx, that solve some of the problem.
    Removing semicolon gave just "?" on screen all the time.
    nothing change when button pushed.
    And yes have changed to 0x30.
    And yes, it was to check that UART Work, i was connecting RX to TX
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I think you should write if (button == 0)...


    Try that...
     
  5. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    could try tomorrow, but dont think that will help
    have allways use the if(!button).
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    I don't use that compiler version so I don't know if "RC1" is correct for a port pin, and without a schematic I don't know if that is the port with the switch...

    But I do know that "if (1)" ill always be true and always execute the transmit code. It's also short enough to be slipped into your code painlessly (with the (!Button) hidden behind a comment).

    I suspect the problem lies with how the UART is set up.

    The statement "if (!Button)" is correct C for the hardware you described. Personally I would "#define Button !RC1" so I could say "if (Button)" to sense when pressed.
     
  7. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Have done some modifications
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include "lcd.h"
    6. #include <string.h>
    7. __CONFIG (CLKOUTEN_OFF & FCMEN_ON & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSC);//XT
    8. __CONFIG (LVP_ON & LPBOR_OFF & BOREN_ON & STVREN_ON & WRT_OFF);
    9. #define _XTAL_FREQ 4000000
    10. #define button RC1
    11. void usrt_init (void)
    12. {
    13.     TRISB5=1; // in
    14.  TRISC1=1; // in
    15.  TRISB7=0; // out
    16.     ANSELB=0; // analog off.
    17.    
    18.     //TXSTA    //CHECK THE DATA SHEET FOR TXSTA
    19.     CSRC=0; //Clock Source Select bit not used in async.
    20.     TX9=0; // 9-bit Transmit Enable bit
    21.     TXEN=1; // Transmit Enable bit(1)
    22.     SYNC=0; //1 = Synchronous mode 0 = Asynchronous mode
    23.     SENDB=0; //Send Break Character bit 1 = Send Sync Break on next transmission (cleared by hardware upon completion)
    24.     //0 = Sync Break transmission completed
    25.     BRGH=0; //High Baud Rate Select bit 0= low
    26.     TRMT=1; //Transmit Shift Register Status bit 1 = TSR empty 0 = TSR full
    27.     TX9D=0; //Ninth bit of Transmit Data
    28.    
    29.    
    30.     //RCSTA    //SEE THE DATA SHEET FOR RCSTA
    31.     SPEN=1; //Serial Port Enable bit
    32.     RX9=0;  // 8bit
    33.     SREN=0; // Single Receive Enable bit
    34.     CREN=1; //1 = Enables receiver
    35.     ADDEN=0; //Not Used.
    36.     FERR=0; //Framing Error bit
    37.     OERR=0; // Overrun Error bit
    38.     RX9D=0; // Ninth bit of Received Data
    39.    
    40.    
    41.     BRGH=0;            //  low baud rate  
    42.     SPBRGL=0b11001111;      //baud rate 300
    43.     SPBRGH=0b00000000;
    44.    
    45. }//
    46. void main (void)
    47. {    
    48.     usrt_init();
    49.     while(1) //loop
    50.      {
    51.       //if (!button) // if pressed the button.
    52.      // {
    53.        TXEN=1;
    54.         TXREG = 0x30;//1; //sends a number
    55.      
    56.            while(TRMT) // until empty.
    57.       {
    58.       }
    59.       }
    60.      //}
    61. }  
    62.  
    63.    
    64.  
    I make it send the number 0x30 back to pc, all the time, without any button pressed,.
    just to illiminate that part.
    Still nothing is comming to PC.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What kind of USB dongle do you use for RS232 interface. Could it be that you need RS232 level translator. And I also think we need a schematic. To save time just use paper and pen. Then take a photo and post that.
     
  9. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Use the Pickit2 dongle as interface, and if i set out/input together the log screen Works, then i think the transmission to and from PC is ok.

    Schematic, sure, but from start this is just the Chip, and nothing more.
    TX and RX tied to the interface- like in the document. page 69 . to pickit2 manual-
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    One thing I do very early on in a PIC project is verify I have the clock speed I think I have. While my preferred method is to bang a port pin at the instruction rate and view it on a scope, other methods such as making a 1 second pause and toggling a LED can also be effective.

    In your specific case I do believe you left out the OSCCON setting to actually get the 4MHz clock you need:

    Code ( (Unknown Language)):
    1.    OSCCON = 0b01101000;
    Without this setting the oscillator will default to 500KHz.
     
    MMcLaren likes this.
  11. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4

    Thanx a lot, stupid me,
    Now it Works, and added a button more, just to show i got different data return from chip.

    Added proof :)

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include "lcd.h"
    6. #include <string.h>
    7. __CONFIG (CLKOUTEN_OFF & FCMEN_ON & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSC);//XT
    8. __CONFIG (LVP_ON & LPBOR_OFF & BOREN_ON & STVREN_ON & WRT_OFF);
    9. #define _XTAL_FREQ 4000000
    10. #define button1 RC1
    11. #define button2 RC2
    12. void usrt_init (void)
    13. {
    14.     TRISB5=1; // in
    15.  TRISC2=1; // in
    16.  TRISB7=0; // out
    17.     ANSELB=0; // analog off.
    18.     ANSELC=0;
    19.     IRCF3=1;//4MHz clock speed
    20. IRCF2=1;
    21. IRCF1=0;
    22. IRCF0=1;
    23.     //TXSTA    //CHECK THE DATA SHEET FOR TXSTA
    24.     CSRC=0; //Clock Source Select bit not used in async.
    25.     TX9=0; // 9-bit Transmit Enable bit
    26.     TXEN=1; // Transmit Enable bit(1)
    27.     SYNC=0; //1 = Synchronous mode 0 = Asynchronous mode
    28.     SENDB=0; //Send Break Character bit 1 = Send Sync Break on next transmission (cleared by hardware upon completion)
    29.     //0 = Sync Break transmission completed
    30.     BRGH=0; //High Baud Rate Select bit 0= low
    31.     TRMT=1; //Transmit Shift Register Status bit 1 = TSR empty 0 = TSR full
    32.     TX9D=0; //Ninth bit of Transmit Data
    33.    
    34.    
    35.     //RCSTA    //SEE THE DATA SHEET FOR RCSTA
    36.     SPEN=1; //Serial Port Enable bit
    37.     RX9=0;  // 8bit
    38.     SREN=0; // Single Receive Enable bit
    39.     CREN=1; //1 = Enables receiver
    40.     ADDEN=0; //Not Used.
    41.     FERR=0; //Framing Error bit
    42.     OERR=0; // Overrun Error bit
    43.     RX9D=0; // Ninth bit of Received Data
    44.    
    45.    
    46.     BRGH=0;            //  low baud rate  
    47.     SPBRGL=0b11001111;      //baud rate 300
    48.     SPBRGH=0b00000000;
    49.    
    50. }//
    51. void main (void)
    52. {    
    53.     usrt_init();
    54.     while(1) //loop
    55.      {
    56.       if (!button1) // if pressed the button.
    57.       {
    58.      
    59.         TXREG =0b11110011;// 1; //sends a number
    60.      
    61.            while(!TRMT) // until empty.
    62.       {
    63.       }
    64.       }
    65.        if (!button2) // if pressed the button.
    66.       {
    67.      
    68.         TXREG =0b10000011;// 1; //sends a number
    69.      
    70.            while(!TRMT) // until empty.
    71.       {
    72.       }
    73.       }
    74.      }
    75. }  
    76.  
    77.    
    78.  
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    Congratulations. Don't you love it when a plan comes together? :)
     
  13. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Yes, great,
    But ran into another problem now.
    Code ( (Unknown Language)):
    1.  
    2.  
    3. static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
    4. {
    5.  if(TMR1IF) // Was this a timer overflow?
    6.   {
    7.       TMR1IF=0;//Clear interrupt flag, ready for next
    8.    TMR1L=0x80;
    9.    TMR1H=0x81; //If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
    10.    timer_tick=1;
    11.    sec++;   // tillægger et sekund
    12.     if (sec==60)  // hvis sekund = 60
    13.  { sec=0;    // sekund = 0
    14.         minut_tick=1;   // og plusser et minut
    15.  
    16.  } // slut "if second"
    17.     }
    18.     if(RCIF) // data in uart.
    19.     {
    20.      lcd_clear();
    21.      utoa(sendt, RCREG, 8);
    22.      lcd_puts(sendt);
    23.     }
    24. }// end interrupt
    25.  
    The interrupt for time Works fine,
    Can i just add an "IF" more into the interrupt. like i did with "if(RCIF)"
    then it do the following when it recieves data. ???
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I do not think you need any interrupt in this setting. Use a master/slave setup instead. Another thing, how do you plan to organize the data before sending them? Using a smart protocol can save you a lot of work. Like it is no need to convert data to string before sending them ;)
     
  15. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    How?


    By master slave, what do you mean ?

    The one thing i need to send is simpel as TXREG=TMR0
    The other thing is data from reading af temperature.
    it becomes a number, from 0 to 100 i think,
    no big deal in that.
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  17. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Sure i will send the ADC number( binary ), not the temperature,
    well aware of that,

    with master slave, that is not an option, cause it use to many wires.
     
    Last edited: Apr 27, 2013
  18. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Works now. found that PIC not recieving anything cause, the RX pin is also connected to pin 18
    ICSPCLK , use to program the PIC and TX is also connected to
    ICSPDAT, when program the PIC i removed these 2 wires, then PIC recieves,.
     
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    So all work as it should now?
     
  20. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    hopefully, need to get i divided into to chip now, the one outside should have rain, and temperature.
    when the one inside sends a "00" to the outside, it reset the rain. and if "01" then it should return the amount of rain in timer0. and when "02" send it should send the temperature back....
    it Works, when i send the commands from PICKIT2 , so i think i easy can get it to Work.

    one Q, what will be the best way to protect the outside chip from Water, can build it inside, but muisture can allways get in,
     
Loading...