Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

PIC Microcontroller :: Selecting Random PORTs btis

Posted by Circuit Negma on December 4, 2007


The following is a code that would allow for selecting PORTS pins randomly.

/*
Created By: Circuit Negma
Date : Dec 04, 2007
File : random.c
*/

/*———————————————————————*/
/* define the PIC16F876A directive */
/*———————————————————————*/
#include <pic.h> // Will define <pic16f87xx.h>
#include <STDLIB.H> // Will provide us with the random function generator

/*
Microprocessor : PIC16F87xx
Clock Speed : 20MHz
Hardware Configuration : 0x3F76
*/

/*———————————————————————*/
/* define Global variables */
/*———————————————————————*/
unsigned char LEDTime; // This is the LED heartbeat indicator for PIC
unsigned char new_i_random; // The new generated random number (PORT pin selector)
unsigned char old_i_random; // The old generated random number


/*———————————————————————*/
/* Run Once Initialization */
/*———————————————————————*/
void InitProc()
{
// init system variables

ADCON1 = 0X07; // PORTA is set to digital pins
PORTA = 0XFF; // Clear PORTA
TRISA = 0X00; // Set PORTA direction to output

PORTB = 0x00; // Clear PORTB
TRISB = 0x00; // Set PORTB direction to output

PORTC = 0x00; // Clear PORTC
TRISC = 0x00; // Set PORTC direction to output

// Set the OPTION_REG
// OPTION_REG<7> PORTB pull-ups disabled
// OPTION_REG<6> Interrupt on rising edge of RB0/INT pin
// OPTION_REG<3> Prescaler assigned to WDT
// OPTION_REG<2,1,0> Set to 110, TMR0 prescaler 1:128, WDT prescaler 1:64
OPTION = 0xC6;
INTEDG = 0;

TMR1L = 0xC0; // Pre load timer 1 (value in nteag0.h)
TMR1H = 0x00; // PRe load timer 1 (value in nteag0.h)
T1CON = 0x31; // Enable timer 1, prescaler to 1:8

// Init local var.
//LEDTime = TIMER1_LED_DELAY; // Init LED countdown 25ms * 20 =~ 500ms delay
LEDTime = 20;

// Setup interrupts
TMR1IE = 0; // Enable timer1 interrupt PIE1<0>
GIE = 0; // Enable global interrupts INTCON<7>
PEIE = 0; // Enable peripheral interrupts INTCON<6>
INTE = 0;
}

/*———————————————————————*/
/* main program */
/*———————————————————————*/
void main(void)
{
InitProc();

new_i_random = 10; // Initialize new random number to something bigger than 7
old_i_random = 0; // Initialize old random number

while(1) // PIC will loop for infinite
{
while((new_i_random > 7) && (old_i_random != new_i_random)) // Make sure that the pin selector is within the range of bits of PORTC
{
srand((TMR0*new_i_random)); // Reseed the random function generator
new_i_random = rand(); // Generate a new random number
new_i_random &= 0x0F; // Select the low bits of the new generated number
}

PORTC &= 0x00; // Clear the PORT
PORTC = 1 << new_i_random; // Shift “new_i_random” bits to the left to enable a pin of PORTC

asm(“nop”);

old_i_random = new_i_random; // Store the old PORTC pin

new_i_random = 10; // Reset the PORTC pin selector

CLRWDT(); // Clear the watchDog timer of the PIC before the PIC resets
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: