View previous topic :: View next topic |
Author |
Message |
shhn
Joined: 14 Jun 2010 Posts: 4
|
16F876 random number |
Posted: Mon Jun 14, 2010 8:38 am |
|
|
i want to generate random number from 16f876 every 1 seconds and i want to read rs232 this numbers. how can i write this code? please help |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Mon Jun 14, 2010 8:41 am |
|
|
When is your homework assignment due? |
|
|
shhn
Joined: 14 Jun 2010 Posts: 4
|
|
Posted: Mon Jun 14, 2010 8:48 am |
|
|
no i m not student, only I m interested pic and I want to develop some projects with pic
also i now c and cpp but in this issue i have is missing |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Mon Jun 14, 2010 9:10 am |
|
|
You can try this:
Code: |
#include <16F876.h>
#include <math.h>
#use delay(clock=8000000)
#fuses INTRC,NOWDT,PUT,NOBROWNOUT,NOLVP
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
void main() {
int8 random_number;
while{1}
{
random_number = (int8)(10*randn()); //generate a random number between zero and 10
printf("\n\rThis is a random nymber: %u", random_number);
delayh_ms(1000); //prints every 10 seconds
}
}
| THis uses the internal oscillator at 8Mhz. Make sure you have a MAX232 or equivalent RS232 chip connected via hardware UART pins of the PIC (C6 and C7).
Look at this thread to see how to hook up the hardware.
http://www.ccsinfo.com/forum/viewtopic.php?t=42784 |
|
|
shhn
Joined: 14 Jun 2010 Posts: 4
|
|
Posted: Mon Jun 14, 2010 11:45 am |
|
|
Thanks for your reply.
I compiled your code and put in pic, now, I want to test pic. How to generate random number? How can I do this? |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Mon Jun 14, 2010 11:48 am |
|
|
What kind of hardware do you have? IF you want to display something on a PC (like on hyperterminal) you need to make a serial cable. You will need something similar to the schematic I linked to in my previous post. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Jun 14, 2010 11:55 am |
|
|
There is a built in function that can generate random numbers, it use stdlib.h where you can define
the maximum number (RAND_MAX) to generate.
Humberto |
|
|
shhn
Joined: 14 Jun 2010 Posts: 4
|
|
Posted: Mon Jun 14, 2010 12:03 pm |
|
|
mkuang wrote: | What kind of hardware do you have? IF you want to display something on a PC (like on hyperterminal) you need to make a serial cable. You will need something similar to the schematic I linked to in my previous post. |
i have this USB PIC Programmer (D149BC). i connect it with usb cable.
|
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Mon Jun 14, 2010 12:41 pm |
|
|
The silver connector on the lower left corner is an ethernet connector right? And you connect that to the programmer?
What programmer do you have? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jun 14, 2010 1:43 pm |
|
|
mkuang wrote: | The silver connector on the lower left corner is an ethernet connector right? And you connect that to the programmer?
What programmer do you have? |
It looks like USB to me. I think this IS his programmer. What I would want is a schematic of the PIC target, the board that the PIC runs on. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|