Jump to content
  • 0

Usart not working


btb4198

Question

I just keep getting 0 bytes and I do not know why.
I am using a ChipKit uC32 board and I have a Sparkfun RS232 Shifter connected to my computer.
I have RX from the Shifter wired to TX on the uC32 and TX wired to RX. and I see the light blinking for RX but it is not working.
 
When I use HyperTerminal, I connect to Com8( and I check and that is the right comport) .
I checked Device Manager, and the comport is set to 8 and Data bits is 8, Parity is None, Flow control is None, and Stop bits is 1.
and I set HyperTerminal so the same thing.
ok here is my code: 

 

float Acceptable_Water_Range;
/*
 * 
 */
int main(int argc, char** argv)
{
    Acceptable_Water_Range = 1;
     WCM_Range = '0'; 
    int FPb = 80000000; // peripheral bus clock frequency set at max SYSCLK
    
    int desired_baud = 19200;
    unsigned int BRG = (FPb / 2 / desired_baud) - 2;
TRISBbits.TRISB0 = 0; //set portB pin to output
TRISBbits.TRISB2 = 0;//set portB pin to output
TRISDbits.TRISD9 =0; //set portB pin to output
 
//OSCConfig( OSC_POSC, OSC_PLL_MULT_24, OSC_PLL_POST_256, OSC_FRC_POST_64 );
//OSCSetPBDIV( OSC_PB_DIV_8 );
OpenUART1(UART_EN| UART_NO_PAR_8BIT | UART_1STOPBIT , UART_RX_ENABLE|UART_TX_ENABLE, (FPb /16/desired_baud) - 1 );
//OpenUART2(UART_EN| UART_NO_PAR_8BIT | UART_BRGH_SIXTEEN , UART_RX_ENABLE|UART_TX_ENABLE, (FPb / (16*desired_baud) - 1 ));
 SpiChnOpen(1, SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_ON, BRG);
 OpenSPI1( MASTER_ENABLE_ON|SPI_MODE32_ON,SPI_ENABLE);
 SetUP_AtoD();
turn_On_LED1();
 CS = 1;
  while(BusyUART1()); 
    putsUART1("******** Main Menu ******** \r\n");
 
 
  Write_Usart( "************************** \r\n");
  Write_Usart( "****Flow Computer********* \r\n");
while(1)
{
    Write_Usart( "******** Main Menu ******** \r\n"); 
    Write_Usart( "1) Set-up \r\n");
    Write_Usart( "2) Run \r\n");
   char temp ='1'; // read_Usart(1);
    switch ( temp){
             case 1:
                 configuration_BSW();
                // configuration_Range();
                 break;
                 case 2:
                     Run();
                     break;
    }
    
}
 
//U1MODESET = 0x8000; // enable UART1

    return (EXIT_SUCCESS);
}

 
Can anyone help me ?

 

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

Hi btb4198,

This is a bit weird. If the RX light is blinking on the UC32, then data is definitely getting sent to your USART port. Have you checked to make sure that your serial terminal is configured correctly with a 19200 baud rate?

Link to comment
Share on other sites

ok I  tested the SparkFun RS232 Shifter - SMD with HyperTerminal and that is  working. ok did I set my openUsart correctly?
I think that is the problem:
int FPb = 80000000; // peripheral bus clock frequency set at max SYSCLK

int desired_baud = 19200;
unsigned int BRG = (FPb / 2 / desired_baud) - 2;
//TRISBbits.TRISB0 = 0; //set portB pin to output
//TRISBbits.TRISB2 = 0;//set portB pin to output
//TRISDbits.TRISD9 =0; //set portB pin to output

OSCConfig( OSC_POSC, OSC_PLL_MULT_24, OSC_PLL_POST_256, OSC_FRC_POST_64 );
OSCSetPBDIV( OSC_PB_DIV_8 );
OpenUART1(UART_EN| UART_NO_PAR_8BIT | UART_BRGH_SIXTEEN , UART_RX_ENABLE|UART_TX_ENABLE, (FPb / (16*desired_baud) - 1 ));

Link to comment
Share on other sites

Hi btb4198,

I'm by no means an expert, but taking a look at the OpenUART1, although you are calculating the baud rate as if you are using a Peripheral Bus Clock frequency of 80 MHz, the uC32 calculates it by what the actual frequency is within the registers. Currently, (and by default) you have the Peripheral Bus Clock set at 10 MHz, resulting in your UART clock running at 75 baud. 

I *think* you can change this to your desired value in one of two ways (again, I'm not an expert). You could leave the PBCLK at 10 MHz and instead supply your last parameter in OpenUART1 to be 32 (as per page 14 of this Microchip document), rather than the 259 or 260 you currently have (depending on which code you're looking at- the originally posted code, or your latest comment code).

Alternatively, you could change the PBCLK to match the system clock of 80 MHz by changing OSCSetPBDIV to be: OSCSetPBDIV( OSC_PB_DIV_1 ); but I do not know the impact that will have on the rest of the system functions.

Let me know if you have any more questions.

Thanks,
JColvin

Link to comment
Share on other sites

ok I tried this:

//OSCConfig( OSC_FRC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_64 );
//OSCSetPBDIV( OSC_PB_DIV_8 );
OSCSetPBDIV( OSC_PB_DIV_1 ); 
OpenUART1(UART_EN| UART_NO_PAR_8BIT | UART_1STOPBIT , UART_RX_ENABLE|UART_TX_ENABLE, 32);

 

and it did not work

Link to comment
Share on other sites

Hi btb4198,

I'll need a little more information before I can help you further.

What software program are you using to program the uC32 board? MPLAB X? What compiler are you using?
Is the code you posted initially your full code? You don't appear to include any header files there.
When you say it does not work, what do you mean? Did it not compile and give you an error? Or is the program seemingly running, but you are not getting the results you expected?

Also, I do not know if you are dedicated to using your current software, but using MPIDE or the Arduino IDE with the chipKIT Core would make a lot of this initialization a lot easier. To get the default UART port set up for 19200 baud, you would just need the line: Serial.begin(19200);

Thanks,
JColvin

Link to comment
Share on other sites

I am using  MPLAB X, I am using the PiCkit3 and XC32(v1.40) :

/* 
 * File:   main.c
 * Author:
 *
 * Created on March 10, 2016, 7:49 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include "p32xxxx.h"
#include "lega-c\plib.h"
#include "lega-c\peripheral\adc10.h"

#define BAUDRATE 115200
//#define PERIPHERAL_BUS_F    (80000000ul)
//#define FPb 80000000       // peripheral bus clock frequency set at max SYSCLK
//#define BAUDRATE           (19200ul)
 
#define CS     LATBbits.LATB0 // chip select 
char WCM_Range;
#define conversion_factor 0.0000762939453125 // (5V/ 65536B)
/*
 * 
 */
float Acceptable_Water_Range;
int main(int argc, char** argv) {
 Acceptable_Water_Range = 1;
     WCM_Range = '0'; 
    int FPb = 80000000; // peripheral bus clock frequency set at max SYSCLK
    
    int desired_baud = 19200;
    unsigned int BRG = (FPb / 2 / desired_baud) - 2;
TRISBbits.TRISB0 = 0; //set portB pin to output
TRISBbits.TRISB2 = 0;//set portB pin to output
TRISDbits.TRISD9 =0; //set portB pin to output
 
//OSCConfig( OSC_FRC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_64 );
//OSCSetPBDIV( OSC_PB_DIV_8 );
OSCSetPBDIV( OSC_PB_DIV_1 ); 
OpenUART1(UART_EN| UART_NO_PAR_8BIT | UART_1STOPBIT , UART_RX_ENABLE|UART_TX_ENABLE, 32);//(FPb /16/desired_baud) - 1 );
//OpenUART2(UART_EN| UART_NO_PAR_8BIT | UART_BRGH_SIXTEEN , UART_RX_ENABLE|UART_TX_ENABLE, (FPb / (16*desired_baud) - 1 ));
 SpiChnOpen(1, SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_ON, BRG);
 OpenSPI1( MASTER_ENABLE_ON|SPI_MODE32_ON,SPI_ENABLE);
 //SetUP_AtoD();
turn_On_LED1();
 CS = 1;
  while(BusyUART1()); 
    putsUART1("******** Main Menu ******** \r\n");
 
 
  Write_Usart( "************************** \r\n");
  Write_Usart( "****Flow Computer********* \r\n");
while(1)
{
    Write_Usart( "******** Main Menu ******** \r\n"); 
    Write_Usart( "1) Set-up \r\n");
    Write_Usart( "2) Run \r\n");
   char temp ='1'; // read_Usart(1);
    switch ( temp){
             case 1:
                 configuration_BSW();
                // configuration_Range();
                 break;
                 case 2:
                     Run();
                     break;
    }
    
}
 
//U1MODESET = 0x8000; // enable UART1

    return (EXIT_SUCCESS);
}

void Modbus_Send(void)
{
    unsigned char Message[8] ;
    Message [0] = 1;
    Message [1] = 3;
    Message [2] = 12;
    Message [3] = 130;
    Message [4] = 0;
    Message [5] = 1;
   // CRCfunction(Message, 6);
     while(BusyUART2());
    
    putsUART2(Message);
    return;
    
}

/*void CRCfunction(unsigned char* array_Out, int bytelen)
        {
           unsigned short crc1 = 0XFFFF; // 1111 1111 1111 1111 also 2bytes also 65535
            for (int e = 0; e < bytelen; e++)
            {
                crc1 ^= (unsigned short)array_Out[e];
                for (int i = 8; i != 0; i--)
                {
                    if ((crc1 & 0x0001) != 0)
                    {
                        crc1 >>= 1;
                        crc1 ^= 0xA001;
                    }
                    else
                        crc1 >>= 1;
                }

            }
            array_Out[6] = (crc1 & 0x00FF);//185//Convert.ToByte(crc1 & 0x00FF);
            array_Out[7] = ((crc1 & 0xFF00) / 256);//135;// Convert.ToByte(( crc1 & 0xFF00) / 256);

}*/
void configuration_BSW(void)
{
    Write_Usart( "******** Configuration Menu ******** \r\n"); 
    Write_Usart( "******** 1) WCM 7300M (0-5%)  ******** \r\n"); 
    Write_Usart( "******** 2) WCM 7300M (0-10%)  ******** \r\n"); 
    Write_Usart( "******** 3) WCM 7300M (0-25%) ******** \r\n"); 
  //  WCM_Range = read_Usart(1);
    
}

/*void configuration_Range(void)
{
  Write_Usart( "******** Acceptable percent of water ******** \r\n");  
   // char temp = read_Usart(1);
   Acceptable_Water_Range = atof(temp);
}
*/
void Run(void)
{
  Write_Usart( "******** Run Menu ******** \r\n"); 
    Write_Usart( "1) Start \r\n");
    Write_Usart( "2) Stop \r\n");
    Write_Usart( "3) receipt  \r\n");
  //  char temp = read_Usart(1);
    switch ('1'){
             case 1:
                 
                 break;
                 case 2:
                     Run();
                     break;
        case 3:
            break;
    }
}

void Start(void)
{
    
}


void  WCM_7300M_0to5(void)
{
    int AtoD_temp = read_AtoD();
    float voltage_temp = conversion_factor * AtoD_temp;
    float percent_temp = 1 * voltage_temp;
    if ( percent_temp <=  Acceptable_Water_Range )
    {
      turn_On_LED1();  
    }
    else
    {
      turn_OFF_LED1();
    }
    
}
void Main_Menu(void)
{
    Write_Usart( "******** Main Menu ******** \r\n");
      Write_Usart( "1) Start \r\n");
      Write_Usart( "2) Stop \r\n");
      
    // char temp = read_Usart(1);
     switch ('1'){
             case 1:
                 
                 break;
                 case 2:
                     
                     break;
}
}

char Write_SPI(char temp)
{
  CS = 0;                    // select chip
    SPI1BUF = temp;                     // write to shift register to begin transmission
    while( !SPI1STATbits.SPIRBF);    // wait for transfer to complete
    return SPI1BUF;                  // read the shift register value
  CS = 1;                 // release chip
}
int Read_SPI()
{
  CS = 0;                    // select chip
     int temp = DataRdySPI1();                   // write to shift register to begin transmission
    while( !SPI1STATbits.SPIRBF);    // wait for transfer to complete
    return temp;                  // read the shift register value
  CS = 1;                 // release chip
}

void turn_On_LED1(void)
{
  
     LATBbits.LATB2 = 1;
}
void turn_OFF_LED1(void)
{
     LATBbits.LATB2 =  0;
}

void turn_On_LED2(void)
{
     LATDbits.LATD9 =1;
}
void turn_OFF_LED2(void)
{
    
     LATDbits.LATD9 =0;
}


void Write_Usart( char temp[])
{
    
    while(BusyUART1());
    
    putsUART1(temp);
   
}

/*
char* read_Usart( int count )
{
    char read_temp[];
    while(DataRdyUART1());
     int numberOfBytes = getsUART1( count,*temp,123);
     return temp;
} */


int getcU1( char *ch_ptr)
{
 if( !DataRdyUART1()) // wait for new char to arrive
 return FALSE; // Return new data not available flag
 else
 {
 *ch_ptr = ReadUART1();// read the char from receive buffer
 return TRUE; // Return new data available flag
 }
}// end of getcU1

int read_AtoD(void)
{
    while(BusyADC10());
    int result = ReadADC10(1);

    return result;
}

 

Link to comment
Share on other sites

Hi btb4198,

I don't know if this will solve the issue, but I did find a post on a Microchip forum indicating that the putsUART1 function you call inside of your Write_Usart needs different sorts of parameters (link to relevant post on the forum thread).

Thanks,
JColvin

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...