Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Urgent help on UART programing of pic16f877a in mikroC

Status
Not open for further replies.

pic16f877

Newbie level 6
Joined
Feb 16, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
tezpur university
Activity points
1,388
hii friend, i am a b.tech final year student... i need some help in the pic16f877a uart programing.

actually i am doing research on real time ECG prototype...so i need to transfer the collected ECG data from my body through pic16f877a to the uart continuously through rs232 cable ... i used to write a program by my own..lets get me ensure that is it will work or not....(m using MikroC)

#include "built_in.h"

unsigned int temp_res;

void main() {
UART1_Init(57600); // Initalize USART (9600 baud rate, 1 stop bit, ...
//Delay_ms(100); // wait uart module to set
//TXSTA=0X20;
//RCSTA=0X90;
ADCON1 = 0; // All porta pins as analog, VDD as Vref
TRISA = 0xFF; // PORTA is input
TRISB=0;
//TRISC = 0x3F;

do {


temp_res = ADC_Read(0); // Read ADC results and send via UART
UART1_Write(Lo(temp_res)); // Send lower 8 bits to UART
UART1_Write(Hi(temp_res)); // Send upper 8 bits to UART
PORTB = temp_res;

Delay_ms(2000);
} while (1); // endless loop
}
 

Code:
temp_res = ADC_Read(0); // Read ADC results and send via UART
UART1_Write(IntToStr(Lo(temp_res))); // Send lower 8 bits to UART
UART1_Write(IntToStr(Hi(temp_res))); // Send upper 8 bits to UART
PORTB = temp_res;

I think this will work Properly..
You are passing Interger Data..

---------- Post added at 11:02 ---------- Previous post was at 10:59 ----------

Code:
temp_res = ADC_Read(0); // Read ADC results and send via UART
UART1_Write(IntToStr(Lo(temp_res))); // Send lower 8 bits to UART
UART1_Write(IntToStr(Hi(temp_res))); // Send upper 8 bits to UART
PORTB = temp_res;

I think this will work Properly..
You are passing Interger Data..


Its better you transfer your ADC data as follow:-

Code:
temp_res = ADC_Read(0); // Read ADC results and send via UART
UART1_Write_Text(IntToStr(temp_res));

And what you want to do with this line

PORTB = temp_res;

temp_res is 10-Bit data while PORTB is of 8-Bit
 
actually i want to blink the LED connected to the PORTB pins... whenever the analog data found in the channel AN0 ,that is connected to the output of the ECG signal conditoning ckt, it will show the digital output in the LEDs..

UART communication is linked up but I am not able to blink the LEDs.. please solve me out..
 
Last edited:

is your UART communication is working properly? are you getting ADC data on hyperterminal?

Code:
//After Void Main write this
ADCON0 = 0x81;     //Initialize A/D Converter
ADCON1 = 0xC0;


//At last write this
PORTB = Lo(temp_res);
PORTC = Hi(temp_res);  //You can connect two led's at some other port..


---------- Post added at 12:35 ---------- Previous post was at 12:34 ----------

is your UART communication is working properly? are you getting ADC data on hyperterminal?

Code:
//After Void Main write this
ADCON0 = 0x81;     //Initialize A/D Converter
ADCON1 = 0xC0;


//At last write this
PORTB = Lo(temp_res);
PORTC = Hi(temp_res);  //You can connect two led's at some other port..
 

Thanks for the replay abhishekdixit...

but it still not blinking the LEDs connected to the PORTB and PORTC.. i have modified the code as follows... but still not working... but UART recieves the data continuously..

unsigned int temp_res;

void main() {
UART1_Init(57600); // Initalize USART (9600 baud rate, 1 stop bit, ...
//Delay_ms(100); // wait uart module to set
//TXSTA=0X20;
//RCSTA=0X90;
ADCON0 = 0x81; //Initialize A/D Converter
ADCON1 = 0xC0;
//ADCON1 = 0; // All porta pins as analog, VDD as Vref
TRISA = 0xFF; // PORTA is input
TRISB=0;
TRISC = 0xFC; // RC0 & RC1 as outputs

do {


temp_res = ADC_Read(0); // Read ADC results and send via UART
UART1_Write(Lo(temp_res)); // Send lower 8 bits to UART
UART1_Write(Hi(temp_res)); // Send upper 8 bits to UART
PORTB = Lo(temp_res);
PORTC = Hi(temp_res);

Delay_ms(2000);
} while (1); // endless loop
}
 

Can you Post your Circuit Diagram..

Then i can help you...

Code:
UART1_Write(Lo(temp_res)); // Send lower 8 bits to UART
UART1_Write(Hi(temp_res)); // Send upper 8 bits to UART
Why u have used this

Use this Line
UART1_Write_Text(IntToStr(temp_res));
 

thanks arunsharma0731.....

UART1_Write_Text(IntToStr(temp_res));

this line works as same as my code.... my uart recieveing data but i dont know why adc reading will not shown in portB and portC ... ryt now am unable to send you the ckt diagram.. evening i will attach the ckt diagram...

but i have made a code for adc reading for the same port to display LED s ...it will work properly...
i used to connect a potentiometer in pin AN0... and for different analog votlage in tha pin showed different digital output by blinking LEDs ..so i think there is any problm in the ckt..

that program is....

unsigned int adc_rd;

void main() {
int temp_res;
ADCON1 = 0x8E; // Configure AN0 pin as analog
TRISA = 0xFF; // PORTA is input
TRISC = 0x3F; // Pins RC7, RC6 are outputs
TRISB = 0; // PORTB is output

do {
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
PORTB = temp_res; // Send lower 8 bits to PORTB
PORTC = temp_res >> 2; // Send 2 most significant bits to RC7, RC6
} while(1);
}

(in the UART i use RC0 & RC1 to connect two high bit LEDs)
 

thanks arunsharma0731.....

UART1_Write_Text(IntToStr(temp_res));

this line works as same as my code.... my uart recieveing data but i dont know why adc reading will not shown in portB and portC ...
Can you tell us what value appears in the PORT B and PORTC? Are they same as what you are transmitting to UART?

Code:
unsigned int adc_rd;

void main() {
  int temp_res;
  ADCON1  = 0x8E;              // Configure AN0 pin as analog
  TRISA  = 0xFF;              // PORTA is input
  TRISC  = 0x3F;              // Pins RC7, RC6 are outputs
  TRISB  = 0;                 // PORTB is output

  do {
    temp_res = ADC_Read(0);   // Get 10-bit results of AD conversion
    PORTB = temp_res;         // Send lower 8 bits to PORTB
    PORTC = temp_res >> 2;    // Send 2 most significant bits to RC7, RC6
  } while(1);
}

Looks like you are continuously reading the ADC value and writing to the PORTB and PORTC. I guess, your compiler might have optimized the variable temp_res. In this case, the compiler may omit the trials of reading the value from ADC. Why don't you use volatile key word for temp_res?

i mean

Code:
volatile unsigned int temp_res;

I just suspect. It may or may not be a problem.

All the best
 

I think there is the Problem with your circuit Diagram..
Pls attach your Circuit Diagram..
Then i can tell something ..

Code:
PORTB = temp_res;
PORTC = (temp_res>>8)

Don't use inbuilt function try the above code and check whether it is working or not.
Give some Delay after UART Initialisation..
And Use While loop not do while loop (Although Do While Loop is okay too)
And Give sufficient delay
after every conversion
Hope you will get right result
 
thanks karthikkrv85 & arunsharma0731


i upload the ckt diagram here below....


Hai

I have simulated your circuit and verified the code too without uart. It is working and LED glows as it suppose to glow for the digital value

here the code

Code:
#include "built_in.h"

unsigned int adc_rd;

void main() 
{
int temp_res;
ADCON0 = 0xC1;
ADCON1 = 0x8E; // Configure AN0 pin as analog
TRISA = 0xFF; // PORTA is input
TRISC = 0x00; // Pins RC7, RC6 are outputs--> you have written 3F.  actually you are suppose to write FC or 00
TRISB = 0x00; // PORTB is output

do {
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
PORTB = Lo(temp_res); // Send lower 8 bits to PORTB
PORTC = Hi(temp_res); // Send 2 most significant bits to RC7, RC6
} while(1);
}


---------- Post added at 09:44 ---------- Previous post was at 09:42 ----------

you can avoid built_in header file and do the changes as arunsharma asked you

Code:
PORTB = temp_res;
PORTC = (temp_res>>8)
 
Last edited:

ok i will try this...

is you simulate my previous code for both UART &ADC value display togather?? is it works??

individually both the code works..but when i used to make UART communication that time PORTB & PORTC LEDs are not showing results...
what the value UART recieves as well as it should show that code in my LEDs..

can you give me code?? it is a very simple code but i dont know why it shows problem..
 

ok i will try this...

is you simulate my previous code for both UART &ADC value display togather?? is it works??

individually both the code works..but when i used to make UART communication that time PORTB & PORTC LEDs are not showing results...
what the value UART recieves as well as it should show that code in my LEDs..

can you give me code?? it is a very simple code but i dont know why it shows problem..

Hai i verified the code and simulated. It displays in the LED as well as in the terminal. But i dont know the terminal value is correct or not. Because it shows ascii value for 0x199.

I have uploaded the code too

Code:
#include "built_in.h"

unsigned int adc_rd;



void main()
{
unsigned int temp_res;
Uart1_Init(9600);
ADCON0 = 0xC1;
ADCON1 = 0x8E; // Configure AN0 pin as analog
TRISA = 0xFF; // PORTA is input
TRISC = TRISC & 0xFC; // Pins RC7, RC6 are outputs
TRISB = 0x00; // PORTB is output
Delay_ms(100);
do
{
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
//UART1_write_text("karthik");
UART1_write(Lo(temp_res)); // Send lower 8 bits to UART
Delay_ms(100);
UART1_write(Hi(temp_res)); // Send upper 8 bits to UART
PORTB = Lo(temp_res); // Send lower 8 bits to PORTB
PORTC = Hi(temp_res); // Send 2 most significant bits to RC7, RC6
}while(1);
}

All the best

---------- Post added at 14:14 ---------- Previous post was at 14:07 ----------

It displays correctly in the LED and UART. i have verified again
 
thanks..

now i have to do these data as string rather than sending it as a integer to the UART.. it is look like my string is---

startXXXXend

here first XX is used for aADC read value (x indicates 8bit binary)
and second XX is used for acellerometer data

is it possible to send data like that.. m just confused..

---------- Post added at 16:54 ---------- Previous post was at 16:53 ----------

ryt now i am not using acellerometer ...it is for future reference..
 

hii ...

actually i have to send microcontrollers internal ADC read data to the matlab continuously with the serial port.. adc is 10 bit.. and we know UART will read only 8 bit at atime.. so we have to split the 10 bit to two 8 bit data...

here i am advised to send the data serially to the uart through the Microcontroller as the following sting so that MATLAB recognize the data.....

start xx end

where first x represents higher order ADC bits & second x represents lower order ADC bits.

now i also have to modify my Microcontroller code ..
 

this is the code i want to write for the above string... but it is not working... can anybody help me..





Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "built_in.h"
 
unsigned int adc_rd;
 
void main()
{
 
char txt1[]="start";
char txt2[]="end";
int i=0;
unsigned char rxarray[18];
//char IoutText[10];
 
unsigned int temp_res;
unsigned char temp_res1;
unsigned char temp_res2;
Uart1_Init(9600);
ADCON0 = 0xC1;
ADCON1 = 0x8E;    // Configure AN0 pin as analog
TRISA = 0xFF;       // PORTA is input
TRISC = TRISC & 0xFC; // Pins RC7, RC6 are outputs
TRISB = 0x00;             // PORTB is output
Delay_ms(100);
do
{
 temp_res = ADC_Read(0);
 //LongWordToStr(temp_res,IoutText); // convert the ADC value to string
 do{
     for(i=0;i<5;i++)
     {
     temp_res1= txt1;
     rxarray[i] = temp_res1;
    
     }
     for(i=5;i<15;i++)
     {
     rxarray[i] = temp_res;
     }
     for(i=15;i<18;i++)
     {
     temp_res2= txt2;
     rxarray[i] = temp_res2;
     }
   }while(i<18);
 
UART1_write(rxarray[i]);
Delay_ms(100);
 
}while(1);
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top