Thread: Random number generator using pic18f4520 in mplab c18 compiler

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    Random number generator using pic18f4520 in mplab c18 compiler


    Hi,


    I tried to use Timer0 to generate 2 random numbers, using PIC18f4520 and c18 compiler in c language. But is not working. Please help me.

    Here is my program.
    Code:
    #include<p18f4520.h>
    #include <stdlib.h>    
    void ISR (void); 
    char i,start; 
    #pragma code L_vector=0x18
    void L_vector(void)
    {
    _asm goto ISR _endasm
    } 
    
    #pragma code
    #pragma interruptlow ISR 
    void ISR(void) 
    {
    if(INTCONbits.TMR0IF)
    { 
    TMR0H=0xC2;
    TMR0L=0xF7;
    i=rand();
    start=(i+TMR0H)%8;
    j=rand();
    end=(j+TMR0L)%8;
    }
    } 
    void main(void)
    { 
    
     INTCONbits.GIEH=0;
    RCONbits.IPEN=1;
    INTCON2bits.TMR0IP=0;
    TMR0H=0xC2;
    TMR0L=0xF7;
    T0CON=0b100000101;
    INTCON=0b11100000;
    
    
    while(1);
    }
    
    Last edited by Mr bean; 08-01-2012 at 07:59 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    j is not declared.
    i is not volatile

    Try setting a flag in your interrupt routine and calling rand from your main()

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Oh and you need to
    Code:
    	
    	// Enable all low priority interrupts
    	INTCONbits.GIEL = 1;
    Have a look at the forums at Microchip and see if you can get a blinking light example and work from that example

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, where are your config bits?

    I think that you should go through a tutorial about how to program the PIC with c language -> A really good site is Gooligum Electronics

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    hi, what do you mean by 'i is not volatile'? is it i have to set 'i=0' in my main? sorry i'm new in programming.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Sorry I'm new in programming
    There is nothing wrong with that.

    "Additionally, any global variable which may be modified by the ISR must be declared as 'volatie', to warn the compiler from eleminating apparently redundant references to those variable in the main program" (Meiklehohn D., 2012, 'Lesson 3: inroduction to Interrupts', 'Introduction to PIC Programming: Programming Mid-Range PICs in C' http://www.gooligum.com.au/tutorials...IC_Mid_C_3.pdf)
    Go to the Gooligum tutorials - They are good. The section that you want to learn is 'Programming Mid-Range PICs in C'.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay you're new to programming. One thing you should learn as soon as possible is that "not working" is the worst possible problem description there is. A lot of people unfortunately use it at first.
    You wouldn't take your car to a mechanic and say that it's "not working" and expect them to figure out that you were talking about the back windscreen wiper not wiping off the rain very effectively.
    Please give details about the problem. Anything you can think of. What does it really do? Even saying whether or not the code compiles for you, which may seem obvious, is really useful to know up front.

    So... now that we have that sorted, what exactly is not working? Are your random numbers not random enough?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    hi, i'm working on this project which require two 7-segment to display random numbers between 0-7. These 7-segments is control by a touch sensor. I've revise my code but it's only display 0,7,4,5 and repeat. It's not what i wanted. This is the code:


    Code:
    #include<p18f4520.h>
    #include <stdlib.h>	
    #define first 	PORTAbits.RA0
    #define middle 	PORTAbits.RA1
    #define last	PORTAbits.RA2
    #define first1 	PORTAbits.RA3
    #define middle1 PORTAbits.RA4
    #define last1	PORTAbits.RA5
    #define control PORTCbits.RC0
    
    
    void ISR (void);
    
    
    char i,start,j,end;
    
    
    #pragma code L_vector=0x18
    void L_vector(void){
    _asm goto ISR _endasm
    }
    
    
    
    
    #pragma code
    #pragma interruptlow ISR
    
    
    void ISR(void)
    
    
    {
    if(INTCONbits.TMR0IF)
    {
    
    
    TMR0H=0xC2;
    TMR0L=0xF7;
    i=rand();
    start=(i+TMR0H)%8;
    j=rand();
    end=(j+TMR0L)%8;
    INTCONbits.TMR0IF=0;
    }
    }
    
    
    void main(void){
    
    
    
    
    i=0; 
    INTCONbits.GIEH=0;
    INTCONbits.GIEL=1;
    RCONbits.IPEN=1;
    INTCON2bits.TMR0IP=0;
    TMR0H=0xC2;
    TMR0L=0xF7;
    T0CON=0b100000101;
    INTCON=0b11100000;
    if(control=1)
    {
    switch(start)
    {
    case 0: 
    		first=0;
    		middle=0;
    		last=0;
            break;
    
    
    case 1:
    		first=1;
    		middle=0;
    		last=0;
    		break;
    case 2:
    		first=0;
    		middle=1;
    		last=0;
    		break;
    
    
    case 3:
    		first=1;
    		middle=1;
    		last=0;
    		break;
    
    
    case 4:
    		first=0;
    		middle=0;
    		last=1;
    		break;
    
    
    case 5:
    		first=1;
    		middle=0;
    		last=1;
    		break;
    
    
    case 6:
    		first=0;
    		middle=1;
    		last=1;
    		break;
    
    
    case 7:
    		first=1;
    		middle=1;
    		last=1;
    		break;
    
    
    default:
    		first=0;
    		middle=0;
    		last=0;
    
    
    
    
    
    
    }
    
    
    
    
    
    
     switch(end){
    
    
    case 0:
    		first1=0;
    		middle1=0;
    		last1=0;
    		break;
    
    
    case 1:
    		first1=1;
    		middle1=0;
    		last1=0;
    		break;
    
    
    case 2:
    		first1=0;
    		middle1=1;
    		last1=0;
    		break;
    
    
    case 3:
    		first1=1;
    		middle1=1;
    		last1=0;
    		break;
    
    
    case 4:
    		first1=0;
    		middle1=0;
    		last1=1;
    		break;
    
    
    case 5:
    		first1=1;
    		middle1=0;
    		last1=1;
    		break;
    
    
    case 6:
    		first1=0;
    		middle1=1;
    		last1=1;
    		break;
    
    
    case 7:
    		first1=1;
    		middle1=1;
    		last1=1;
    		break;
    	
    default:
    		first1=0;
    		middle1=0;
    		last1=0;
    
    
    
    
    
    
    
    
    
    
    
    
    		}
    
    
    }
    
    
    
    
    
    
    }

    I've tried to use the other method which require only rand() and srand function. I'm not sure how to seed it so 1 of the 7-segment display odds number while the other display even numbers.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    OK - First thing is first


    You need to set your config bits -> This sets up thing like the oscillator, watchdog timer - Things that if not set up properly, will make your PIC seem like it is doing random things. Learning how to do this should be your priority. An incorrect setting of LVP (low voltage programing) with a floating pin will lead to a reset every time your hand goes near your microprocessor.


    It's good to see that you are now declaring 'j' and 'end'. However, you need to declare them as "volatile". To help you avoid the strenuous task of a Google search, here is how you do that:
    Code:
    volatile char i, j, start, end;

    If you don't declare them as volatile, bad things will happen. If you want to learn more about why this is so, have a look on google or gooligum.


    Code:
    if(control=1)
    statement should be
    Code:
    if(control==1)

    You shouldn't be writing to PORTAbits.RAn, but rather LATAbits.LATAn


    You're treating main() like it is going to loop forever (like the loop function on the Arduino), but it's not. If you want it to loop, you'll need to use
    Code:
    while(1){ /* Everything you want to loop */ }
    or
    Code:
    for(;;) { /* Everything you want to loop */ }
    It's really a personal choice which one you use.


    As a rule with Microcontrollor programming -> Avoid doing anything in an interrupt that isn't necessary. The usual way to do this is by setting up your program like this:


    When a timer interrupt happens -> Set a flag (i.e. Set a (volatile) variable's value from 0 to 1)


    In your main loop ->


    Code:
    (pseudo/C)
    int main(void)
    {
        /* Set up registers/variable values here */
    
    
        while(1) /* Loop forever */
        {
            if (flag == 1)
            {
                flag = 0; // (Clear flag here)
    
    
                /* Get your random numbers here */
    
    
                /* Use your random numbers here */
    
    
            }
        }
    
    
        return 0;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Help
    By KrazySocoKid in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 08:57 PM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. Random number generator
    By jbone0881 in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 10:20 PM
  4. random number generator
    By begginer in forum C Programming
    Replies: 5
    Last Post: 03-08-2011, 12:12 AM
  5. help with random number generator
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2003, 03:46 PM