Severals problems with BT and PIC

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
33.1.1 EUSART ASYNCHRONOUS


TRANSMITTER

The EUSART transmitter block diagram is shown in

Figure 33-1. The heart of the transmitter is the serial

Transmit Shift Register (TSR), which is not directly

accessible by software. The TSR obtains its data from

the transmit buffer, which is the TXREG register.

33.1.1.1 Enabling the Transmitter

The EUSART transmitter is enabled for asynchronous

operations by configuring the following three control

bits:

• TXEN = 1

• SYNC = 0

• SPEN = 1

All other EUSART control bits are assumed to be in

their default state.

Setting the TXEN bit of the TX1STA register enables the

transmitter circuitry of the EUSART. Clearing the SYNC

bit of the TX1STA register configures the EUSART for

asynchronous operation. Setting the SPEN bit of the

RC1STA register enables the EUSART and

automatically configures the TX/CK I/O pin as an output.

If the TX/CK pin is shared with an analog peripheral, the

analog I/O function must be disabled by clearing the

corresponding ANSEL bit.

As i see it, i must clear the SYNC Bit ??
 

Attachments

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Sorry, i have not your edducation in electronics, but i can so much other thing,
If it was easy, i would have done it,
Now i ran in to trouble and asked for help.

Data sheet have been read several times, but maybe it's cause i dont know all the terms and so,

I know i have done my best, and tried several things,
I have a working setup for a couple years ago, with 2 ps 16F1509 by serial comm, and that works,
Have been studing those files, and think i have done it correct.
 

spinnaker

Joined Oct 29, 2009
7,830
Sorry, i have not your edducation in electronics, but i can so much other thing,
If it was easy, i would have done it,
Now i ran in to trouble and asked for help.

Data sheet have been read several times, but maybe it's cause i dont know all the terms and so,

I know i have done my best, and tried several things,
I have a working setup for a couple years ago, with 2 ps 16F1509 by serial comm, and that works,
Have been studing those files, and think i have done it correct.

Follow the procedure for setting up async mode. It can't get be explained much better that that. You aren't even trying. All you do is just ask questions.

There is also google search.

Here is one resource.

https://deepbluembedded.com/bluetooth-module-hc05-interfacing-pic-microcontroller-tutorial/
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Have been studing the sample, and held up with mine code.
Can't see what i have done wrong, but copy the code and now i have working reciever.
Don't really know why it adds 48 to my number i send from terminal,

Next part is to get it to send data,
And again i have been studing datasheet, and found that this code should work,
But it do not, ;(

Any hint's to what i do wrong ?

Been testing with Pickit2 and use UART tools, connected my HC-06 to it, and could send data from and to it, so connection to phone is working correct.

Could not get the connection working when connected from PICKIT2 to my PIC,
Nor recieve or send works, could be a connection error,
Here is my code.
C:
//
#include    "lcd.c"
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#pragma config FEXTOSC = LP //XT     // External Oscillator mode selection bits (XT (crystal oscillator) above 500kHz, below 4MHz; PFM set to medium power)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)


#define _XTAL_FREQ 16000000
#define FOSC 4000000L

#define back_light RB0
#define led1_on RE0
#define KN1 RC3
#define KN2 RC2
#define KN3 RA7
#define KN4 RA6


#define Skip_ROM             0xCC
#define Convert_T             0x44
#define Read_scratchpad     0xBE

#define Port_18B20             RB5
#define Tx_18B20             TRISB5 = 0
#define Rx_18B20             TRISB5 = 1

#define WAIT1                1000
#define WAIT2                500

#define DS18B20_CONV_TIME    750
#define DS18B20_RESET_PULSE 480
#define DS18B20_WAIT_TIME     60
#define DS18B20_PULLUP_TIME 2

#define ONEWIRE_PRESENT     0
#define ONEWIRE_ABSENT         1

bit modtaget,data_tick,sekund;
int get_value,data_inde,Data_Buffer,run;
unsigned char printchar[10];

// Functions Declarations
void UART_RX_Init(void);
// Globals
uint8_t UART_Buffer = 0; // is a 8bit interger / 1.byte
//--------------------------------


static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{
  
if(TMR1IF)             // Was this a timer overflow?
    {
          TMR1IF=0;        // Clear interrupt flag, ready for next // timer run to 65k, gives 2 sec interrupt
        //TMR1L=0x00;  // overflow every 2 seconds. to prevent to much interrupt.
         //TMR1H=0x80;     // If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
     sekund=1;  
   }

if (RCIF==1)
   
  {
   CREN=0;
     run=0;
     lcd_clear();
     lcd_puts("Modtaget");
     while(RCIF==1)
     {
     UART_Buffer = RCREG; // Read The Received Data Buffer
     run++;
     }
    // This could have been done within the main loop. Since it's not
    // Excessive processing, so it's OK to do it here below
    if(UART_Buffer-48 == 0x00)
        lcd_puts("Davs");
    if(UART_Buffer-48 == 0x01)
        lcd_puts("Farvel");
  
    //RCIF = 0; // Clear The Interrupt Flag  read only

    lcd_goto(0x40); // goto line2

          
utoa(printchar,UART_Buffer-48,10);

lcd_puts(printchar);

utoa(printchar,run,10);
lcd_goto(0x54);
lcd_puts(printchar);
CREN=1;
  }





}


          void setup(void)
{   
           
COSC0=0;
COSC1=0;
COSC2=1; // use second oss.
HFFRQ0=1;
HFFRQ1=0;
HFFRQ2=1; // 16 mhz

TRISA0=1; // NU
TRISA1=1; // NU
TRISA2=1; // NU
TRISA3=1; // NU
TRISA4=1; // input to timer
TRISA5=1; // NU
TRISA6=1; // Button 1
TRISA7=1; // Button 2

TRISB0=0; // Backlight on/off
TRISB1=0; // not used
TRISB2=0; // not used
TRISB3=0; // RS
TRISB4=0; // not used
TRISB5=1;// CHANGE temp reading DS18B20
TRISB6=1; // program
TRISB7=1; // program

TRISC0=1; // Xtal
TRISC1=1; // Xtal.
TRISC2=1; // button 3
TRISC3=1; // button 4
TRISC4=1; // not used
TRISC5=1;  // not used
//TRISC6=0;  //send data
//TRISC7=1; // recieve data

TRISD0=0; // DATA6
TRISD1=0; // DATA7
TRISD2=0; // DATA5
TRISD3=0; // DATA4
TRISD4=0; // NU
TRISD5=0; // NU
TRISD6=0; // NU
TRISD7=0; // ENABLE LCD

TRISE0=0; // LED Out
TRISE1=1; // NU
TRISE2=1; //NU

ANSELA=0b00000000; // set alle digital.
ANSELB=0b00000000;
ANSELC=0b00000000;
ANSELD=0b00000000;
ANSELE=0b0000;
//RXPPS=RC6;
//TXPPS=RC7;
//timer 1 setup
T1CS0=0; // 0110 = SOCS
T1CS1=1;
T1CS2=1;
T1CS3=0;
T1CKPS0=0; // no prescale
T1CKPS1=0;
nT1SYNC=1; // no sync
  
lcd_init();  //lcd's init
lcd_goto(0);
}
      

void UART_RX_Init()
{
BRGH = 1;    // Set For High-Speed Baud Rate
SPBRG = 103;  // Set The Baud Rate To Be 9600 bps
// Enable The Ascynchronous Serial Port
TX1STAbits.SYNC = 0;
SPEN = 1;
// Set The RX-TX Pins to be in UART mode (not io)
TRISC6 = 1;  // As stated in the datasheet
TRISC7 = 1;  // As stated in the datasheet
//--[ Enable UART Receiving Interrupts ]--
RCIE = 1;    // UART Receving Interrupt Enable Bit
PEIE = 1;    // Peripherals Interrupt Enable Bit
GIE = 1;     // Global Interrupt Enable Bit
//------------------
RX9=0; // not 9 bit
TX9=0;
CREN = 1;    // Enable Data Continous Reception
TXEN=1; // enables transmitter
}




void UART_Write(uint8_t data)
{
  while(!TRMT); // if send buffer is full, then clear the bit
  TXREG = data; // moves data to send reg,
}




      
void main (void)
{
     setup();
      UART_RX_Init(); // Initialize The UART in Master Mode @ 9600bps
    
  
    lcd_clear();
   lcd_goto(0x00);
     back_light=1; // turn on light in LCD
    lcd_puts("Line 1 : Hello");
    lcd_goto(0x40);
    lcd_puts("Line 2 : Denmark!");
    lcd_goto(0x14);
    lcd_puts("Line 3 : World");
    lcd_goto(0x54);
   lcd_puts("Line 4 : Goodbye!");
  RE0=1;  // little led on, indicates 2 sec gab
  

    T1RD16=0; // turn on timer1 16 bit read write
    TMR1ON=1; // turn on timer1
    T1GE=0; // gate allways count
   TMR1IE=1;  // enable interrupr for timer1
   GIE=1; // enable global interrupr
   PEIE=1;  // enable interrupr
   __delay_ms(500); // delay

  
  
     while (1)
     {
      
         if (sekund==1)
         { sekund=0;
         (led1_on=!led1_on);
         }
       
         if (!KN1) // for test only
         {
             TXEN=1;  // enables transmitter
        
             lcd_clear();
                       
             UART_Write(0b00011001); // should send something to BT, ex 25
           
            
         }
         if (!KN2) // not used yet
         {
       
         }
       
         if (data_tick==1) // not used yet
         {
             data_tick=0;
          
          }
     
  
         }
     }
 

spinnaker

Joined Oct 29, 2009
7,830
What do you mean "Don't really know why it adds 48 to my number i send from terminal,"????

Exactly what are you doing and what are you seeing?
 

spinnaker

Joined Oct 29, 2009
7,830
The problem is still that i cant send anything from microchip

What do you mean??? When you send data from the Pic, it is not appearing on the terminal?

If s, the advice is similar to what it was 100 times before when you had problems. Write a small program that does nothing but initialize the serial port. Then write a loop that does nothing but send data to the serail port. It should then display on the terminal.

A large program can be difficult to troubleshoot. Break it down into small pieces and it becomes easier. But you have been told that before.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
This is simple,
Still not sending anything to terminal,
The setup works in recieving, ( that part out of file now)
A hint to what could be issue,

C:
#include <xc.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma config FEXTOSC = LP //XT     // External Oscillator mode selection bits (XT (crystal oscillator) above 500kHz, below 4MHz; PFM set to medium power)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)


#define _XTAL_FREQ 4000000
#define FOSC 1000000L




void UART_RX_Init()
{
      ANSELB=0;
  ANSELC=0;
  BRGH = 1; // Set For High-Speed Baud Rate
  SPBRG = 103;//25; // Set The Baud Rate To Be 9600 bps
  // Enable The Ascynchronous Serial Port
  TX1STAbits.SYNC = 0;

  SPEN = 1;
  // Set The RX-TX Pins to be in UART mode (not io)
  TRISC6 = 1; // As stated in the datasheet
  TRISC7 = 1; // As stated in the datasheet
  //--[ Enable UART Receiving Interrupts ]--
// RCIE = 1; // UART Receving Interrupt Enable Bit
// PEIE = 1; // Peripherals Interrupt Enable Bit
// GIE = 1; // Global Interrupt Enable Bit
  //------------------
  TX9=0;
  RX9=0;
  CREN = 1; // Enable Data Continous Reception
TXEN=1; // enable tranmission
}


void main(void)
{
  //--[ Peripherals & IO Configurations ]--
  UART_RX_Init(); // Initialize The UART in Master Mode @ 9600bps


  while(1)
  {
   
   
      TXREG=0b00000011; // number 3 in binary
      TXREG=13; // write CR to activate send
    
      __delay_ms(500);
      TXREG=0x02; // write 2 in hex
      TXREG=13;
      __delay_ms(500);
      TXREG=1;//write 1 in decimal
      TXREG=13;
      __delay_ms(500);
    
   
    
      
  }

}
Moderators note : you can use code=c for highlighting C code
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
Well I don't understand what your are trying to say.


First if you want to display a one on the terminal. You do it like this. TXREG = '1' .

Your TX pin needs to be an output. Your RX pin needs to be an input. You have them both as inputs. TRISC6 should be TRISC6 = 0; If I am reading the datasheet correctly and TX is on RC6.
 
Top