uart library interfering with my code

Beta Testing discussion on mikroC PRO for PIC.
Post Reply
Author
Message
uzk
Posts: 5
Joined: 27 Jun 2014 22:34

uart library interfering with my code

#1 Post by uzk » 09 Jul 2014 16:47

i have to use uart to communicate serially between my system and wifi module. the system takes values from ds1821 digital temperature sensor and an analog humidity sensor . it compares the values with the values entered from keypad by user and turns on/off the actuators to control temperature and humidity also i have to send these values over a wifi module using serial communication. the code works fine as long as i am not sending the data on serial pin c-6 . please kindly help me if any one can find the problem. when ever i use the serial transmission using uart the " THE IF RELATED TO TEMPERATURE SENSOR PUTS THE PIN TO HIGH AND ALWAYS KEEPS THE ACTUATOR ON PIN rc3 .


// Keypad module connections
char keypadPort at PORTD;
// End Keypad module connections
unsigned short options,temp,temp1,temp2;
unsigned int x;
unsigned short kp,hum1,hum2,hum;
char txt1[3], text2[3];

float VREF=5;
float ADCresult=0;
float voltage1, voltage,voltage2;
char voltagetxt[16];
char volttext[16];
int voltage33,humx,tempx;
int value1821;
int temp1821;
char txt1821[7],tempab,humab,error;
int tt1,hh1;
//------------------------------------------------------------//

// ----------------------------------------// LCD module connections //-------------------------------------------
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//-------------------------------------------// temparature function //-----------------------------------------------------
void get_temp()
{
//'Write Status' command
Ow_Reset(&PORTB, 2);
Ow_Write(&PORTB, 2, 0x0C);
Delay_us(120);
//Write data to 'status' register
Ow_Reset(&PORTB, 2);
Ow_Write(&PORTB, 2, 0x06);
Delay_us(120);


//Start Conversion
Ow_Reset(&PORTB, 2);
Ow_Write(&PORTB, 2, 0xEE);
Delay_us(120);
//Read temperature byte
Ow_Reset(&PORTB, 2);
Ow_Write(&PORTB, 2, 0xAA);
temp1821 = Ow_Read(&PORTB, 2);
Delay_us(120);

value1821 = temp1821&0x80;
if(value1821!=0)
{
value1821=temp1821&0x7F;
value1821=-128+value1821;
}
else
{
value1821=temp1821;
}

}
//-----------------------------------------// humidity function //--------------------------------------------------------
void get_hum()
{
ADCresult = ADC_Read(2);
voltage=(ADCresult*VREF);
voltage1 = voltage/1024;
voltage2 = voltage1*100;
voltage33= voltage2;
}
//-----------------------------------------// keypad function //----------------------------------------------------------
void getdata()
{
kp = 0;
do
{
kp = Keypad_Key_Click(); }
while (!kp);

switch (kp) {

case 1: kp = 1; break; // 1
case 2: kp = 2; break; // 4
case 3: kp = 3; break; // 3
//case 4: temp = 4; break; // A
case 5: kp = 4; break; // 2
case 6: kp = 5; break; // 5
case 7: kp = 6; break; // 6
//case 8: temp = 48; break; // B
case 9: kp = 7; break; // *
case 10: kp = 8; break; // 0
case 11: kp = 9; break; // 9
// case 12: temp = 57; break; // C
//case 13: temp = ; break; // 7
case 14: kp = 0; break; // 8
//case 15: temp =68 ; break; // #
//case 16: temp = 67; break; // D
}
}
//----------------------------------------------// user controll function //----------------------------------------------

void user_controll()
{
Lcd_Out(1, 1, "INFANT INCUBATOR"); // Write message text on Lcd
Lcd_Out(2, 1, "User Control");
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"set parameters");
delay_ms(1000);
Lcd_Out(1,1,"Temperature");
Lcd_Out(2,1,"Value:");
//Lcd_Out(1,1,"Anticlock: 1");
//Lcd_Out(2,1,"Clockwise: 2");
getdata();
temp1=kp*10;
getdata();
temp2=kp;
temp=temp1+temp2;
tempx=temp;

Inttostr(tempx,txt1);
Lcd_Out(2,8,txt1);
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Humidity");
Lcd_Out(2,1,"Value");
getdata();
hum1=kp*10;
getdata();
hum2=kp;
hum=hum1+hum2;
humx=hum ;
Inttostr(humx,text2);
Lcd_Out(2,7,text2);
delay_ms(2000);
Lcd_Out(1,1,"testing");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
}
//------------------------------------------// Main //--------------------------------------------------------------------

void main()
{ //ADCON1=0xC4;

TRISA = 0xFF;
TRISC.f3=0;
TRISC.f0=0;
TRISC.f1=0;
PORTC.f3=0;
PORTC.f1=0;
PORTC.f0=0;
TRISB.f3=0;
PORTB.f3=0;

ADC_Init();
Keypad_Init(); // Initialize Keypad
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
user_controll() ;
//-----------------------------------------// sensor controll loop //-----------------------------------------------------
while(1)
{
UART1_Init(9600);
get_hum();
IntToStr(voltage33,volttext);

get_temp();
IntToStr(value1821,txt1821);

/*tt1=value1821;
hh1=voltage33;

IntTostr(hh1,humab);
IntToStr(tt1,tempab);*/

if (humx<voltage33)
{ PORTB.f3=0;
PORTC.f0=1;
PORTC.f1=0;

}
else if (humx>voltage33)
{ PORTB.f3=1;
PORTC.f0=0;
PORTC.f1=1;
}
else {
PORTB.f3=0;
PORTC.f0=0;
PORTC.f1=0;
}

if (tempx<value1821) // this if creating problem it stays on always
{
PORTC.f3=0;
}
else if (tempx>value1821) //..............
{
PORTC.f3=1;
}
else; //.........

Lcd_Out(1,1,txt1821);
Lcd_Out(2,1,volttext);

delay_ms(2000);


UART1_Write_Text(txt1821);
UART1_Write_Text(volttext);
Delay_ms(2000);

}
}

uzk
Posts: 5
Joined: 27 Jun 2014 22:34

Re: uart library interfering with my code

#2 Post by uzk » 10 Jul 2014 10:35

the code works absolutely fine if i remove the serial transmission part please help as my deadline is very close this 1 thing is creating an issue.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: uart library interfering with my code

#3 Post by janni » 10 Jul 2014 12:14

uzk wrote:the code works absolutely fine if i remove the serial transmission part ...
It's not the UART library that's to blame. Use strings of proper length in IntTostr function or it'll keep overwriting variables following the string in RAM.

uzk
Posts: 5
Joined: 27 Jun 2014 22:34

Re: uart library interfering with my code

#4 Post by uzk » 10 Jul 2014 15:06

thanks for the help i gave size to all my strings and it kind of works but not perfect. there is another problem that sometimes the controller take values from the keypad itself without the key being pressed is this also related to the same issue or this is purely a hardware problem.?


x saber x
Posts: 3
Joined: 17 Jul 2014 18:29

Re: uart library interfering with my code

#6 Post by x saber x » 17 Jul 2014 23:35

hi,
to begin, i want to say that my level in english is average, so forgive me for the mistakes..
i am a beginner in C programming and this is the first time i work with mikroC.
so i want to write a code and i need your help ..
the code should do the following :

first : PORTB.f0=1

when i send the letter 'c'
i must receive the data that is on the port 'RA0'

when i send again the letter 'c'
i must receive the data that is on the port 'RA1'

when i send again the letter 'c'
i must receive the data that is on the port 'RA2'

when i send again the letter 'c'
i must receive the data that is on the port 'RA3'

when i send again the letter 'c'
i must receive the data that is on the port 'RA5'

when i send again the letter 'c'
i must receive the data that is on the port 'RE0'

PORTB.f0=0

when i send the letter 'c'
i must receive the data that is on the port 'RA0'

when i send again the letter 'c'
i must receive the data that is on the port 'RA1'

when i send again the letter 'c'
i must receive the data that is on the port 'RA2'

when i send again the letter 'c'
i must receive the data that is on the port 'RA3'

when i send again the letter 'c'
i must receive the data that is on the port 'RA5'

when i send again the letter 'c'
i must receive the data that is on the port 'RE0'

now the code should return to the beginning and expected that i send the letter 'c'
---

I actually have this code

Code: Select all

float rtdv, rtdr, temp,  y;
int msd, isd, i;
char temperature[6];
char uart_rd;

calcule()
{
rtdv=rtdv *5/1024;
rtdv=rtdv/5;
rtdr=rtdv*1000.0/(5.0-rtdv);
y=0.15274-(rtdr-100.0)*0.0002310;
if (y>=0)y = sqrt(y);
temp = (y-0.39083)/(-0.0001155);
bytetostr(temp,temperature);

delay_ms(1);
}

affichage() 
{
UART1_Write_Text(temperature);
strcpy(temperature,"");
}

void main()
{

TRISA = 0xFF;  
TRISE = 0xFF;
TRISB = 0x00;  

PORTB = 1;      

UART1_Init(9600);

while(1)                             
{       portb.f1=1;
        if (UART1_data_ready())
         {
                  uart_rd = UART1_read();
                  if(uart_rd=='c')           
                  {
                        PORTB.f0=1;        

                        for (i=0; i<6 ;i++)
                        {
                         delay_ms(5);          
                         rtdv=adc_read(i);
                         UART1_Write(13);        
                         calcule();              
                         affichage();         
                         }

                        PORTB.f0=~portb.f0;      
                        
                        for (i=0; i<6; i++)
                        {
                         delay_ms(5);
                         rtdv=adc_read(i);
                         UART1_Write(13);        
                         calcule();             
                         affichage();       
                         }
                 }
         }
}
}
but it does not satisfy my need..
in this code, when i sent the letter 'c' i receive all data contained on ports..

could you help me modify this code or write one that works according to algorthme above?

Post Reply

Return to “mikroC PRO for PIC Beta Testing”