CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Hardware UART help??!!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
CyberTron
Guest







Hardware UART help??!!
PostPosted: Fri Apr 23, 2004 2:28 pm     Reply with quote

Does anyone have a code, example, or anything that could help me doing this:

I have a Pic 16f870 (same family as 16f877), and is using PCWH 3.097

My problem:
I want to use hardware uart to transmit an A/D value in a specific time range....like somewhere between 0-60 seconds...this should happen at a random time between this range...and only once..

How would I do that? I mean, I have figured out that the funktion PUTC is very useful for transmitting data through RS232,

my theory is like this:

int32 ad;

main()
{
while(1)
poll();
}


void poll(void)
{
ad=readadc()
putc(ad);
}


....

Of course there should be more code, but is the thought correct? should that code send the a/d value all the time??

How would I do a timer?

/Friendly wishes Mike
Ttelmah
Guest







Re: Hardware UART help??!!
PostPosted: Fri Apr 23, 2004 3:15 pm     Reply with quote

CyberTron wrote:
Does anyone have a code, example, or anything that could help me doing this:

I have a Pic 16f870 (same family as 16f877), and is using PCWH 3.097

My problem:
I want to use hardware uart to transmit an A/D value in a specific time range....like somewhere between 0-60 seconds...this should happen at a random time between this range...and only once..

How would I do that? I mean, I have figured out that the funktion PUTC is very useful for transmitting data through RS232,

my theory is like this:

int32 ad;

main()
{
while(1)
poll();
}


void poll(void)
{
ad=readadc()
putc(ad);
}


....

Of course there should be more code, but is the thought correct? should that code send the a/d value all the time??

How would I do a timer?

/Friendly wishes Mike

putc, only sends one character. You need to use 'printf'. You don't need a int32, for ad (use an int16).

Then:
printf(putc,"AD Value: %4ld/n",ad);

will send the string 'AD Value: xxx' and a line feed, where 'xxx' will be one to four digits, in text form, containing the AD value. Obviously, you don't need to add the 'descriptive' text, but you do need to add some form of seperator, if the receiver is to know where one number ends, and the next starts.
You don't say what clock rate is in use, but I'd suggest programming timer2, to interrupt at perhaps 1/100th second intervals - since you have PCW, use the Wizard to set up a timer, and look at the code it generates. It is not perfect, but will show you how the values change.
Then have a 'global' variable called something like 'time_to_send', and set the limit on the 'rand' function, like this:
RAND_MAX = 60;

Do this in the main code.

Then have an interrupt handler with something like:

Code:

#INT_TIMER2
void tick(void) {
   static tick=100;
   static time=60;
   if (tick) --tick;
   else {
      tick=100;
      if (time) --time;
      else {
          timeto_send=RND();
          time=60;
      }
      if (time_to_send) --timeto_send;
   }
}


Then in the main code loop with:

while (time_to_send) ;

and send the string when this drops through.

What should happen, is that 'tick' counts down on every interrupt to zero. When it reaches zero (once per second), it decrements 'time' when this reaches zero, a random value, is put into 'time_to_send'. If time_to_send is non zero, it is decremented.
In the main code, you wait till 'time_to_send' equals zero, and send the ADC value.

Best Wishes
CyberTron
Guest







PostPosted: Sat Apr 24, 2004 12:42 am     Reply with quote

Thank you for your fast reply!! :D

I will try this today and report my success or failure :D

/Mike
CyberTron
Guest







PostPosted: Sun Apr 25, 2004 2:37 am     Reply with quote

Trouble again!
I�ve done this code:

Quote:

#include <16f870.h>
#include <stdlib.h>
#include <stdio.h>
/* Config f�r PIC-processor*/
#fuses XT, NOPROTECT, NOWDT, HS
#use delay (clock=4000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7)

void Config(void);
void Bevakning(void);
void tick(void);
int time_to_send=0;

void main(void)
{
Config();
while(1)
{
Bevakning();

}
}


void Config(void)
{
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
setup_spi(FALSE);
setup_psp(PSP_DISABLED);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
//setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,255,1);
setup_ccp1(CCP_OFF);
//enable_interrupts(INT_RDA);
enable_interrupts(INT_TIMER2);
enable_interrupts(global);
srand(1);
}


void Bevakning(void)
{
int16 ad=12;
int16 id=5;
int16 random;
float check=0;

if(time_to_send == 0) //som det �r nu s� skickar den 1 char/byte per
{ //g�ng som time_to_send = 0, dvs, den skickar I, v�ntar tills
ad=read_adc(); //time_to_send =0 och skickar D...osv..
check=(float)((id+ad)/2);
printf("ID:%ld AD:%ld Ch:%2f ",id, ad, check);
ad=0;
check=0;
}
}


#INT_TIMER2
void tick(void)
{
static int8 tick=100; //static
static int8 time=60; //static
if (tick != 0)
--tick;
else
{
tick=100;
if (time != 0)
--time;
else
{
time_to_send=rand();
time=60;
}
if (time_to_send !=0)
--time_to_send;
}
}



It seems to work, at least in my simulator (PIC Simulator IDE) but it doesnt look like the random funktion is working correctly?!, because it send only 1 character every 7000mikrosecond, (no random what it seem anyway), is there a faulty timer interrupt??? or is it just me ? (don't have the real stuff yet, not for another week, but does the code seem correct?)

And what can I do so it seends the whole "printf()" at the same time, (not waiting after sending 1 char??) I�ve tried

for(i=0; i<20; i++)
{
printf("1234567890")
i++
}

(not exactly like that by close) but it doesn't work

any ideas??

/Mike
Ttelmah
Guest







PostPosted: Sun Apr 25, 2004 2:51 am     Reply with quote

You need to set 'RAND_MAX' somewhere.
rand returns an integer, in the range 0...RAND_MAX.
I'm not sure what you mean about printf 'waiting'. Printf itself, does not 'buffer', but the hardware UART, gives a couple of characters of buffering. Printf, will send characters till the hardware buffer is full, then sit waiting, and sending each character in turn, as the buffer empties. When the function exits a couple of characters will still be in the hardware buffer, but the whole string will have 'sent' (in the sense that it will have been transferred to the hardware). If you want printf to return immediately, to allow other processing to continue, then you would need to write a soft buffered' driver, to use the serial transmit buffer empty interrupt to transfer data to the hardware.
The 'demo' code you show, should send:
1234567890123456789012345678901234567890.....
You may well have problems doing this, since the PC, could itself suffer a buffer overrun sending so much data!...

Best Wishes
CyberTron
Guest







PostPosted: Sun Apr 25, 2004 8:14 am     Reply with quote

RAND_MAX is set to 60 in STLIB.h

with commando: #define RAND_MAX 60


How would one to the Recieving part (just curious)

let's say that I want to recieve the code I just sent (see code from the other posts)...how would I do that? is it just to use getc()?

/Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group