123PICMCUExperimentsfortheEvilGenius-Help

Thread Starter

mpuvdd

Joined Feb 11, 2007
50
If anyone has this book, could you please help me with experiment 47; Producing Random #'s. Have you gotten this thing to work? Is the code even valid, or what? I've set this up many times, and yet it still fails to work. I know how to use LCD's, but maybe this is just another of Myke's (the author) typos.

Thanks for your consideration,
mpuvdd
 

Thread Starter

mpuvdd

Joined Feb 11, 2007
50
I got this code from http://pic18fusb.online.fr/PicKit2, only because it is not shown in the book.

#include <pic.h>
/* cRandom.c - Display a Random Number Each Time a Button is Pressed

This Program Initializes Hitachi 44780 Based LCD in 4 Bit Mode
and then writes a simple string to it. The simulator was used
to time delay values.

LCD Write Information can be found at http://www.myke.com

RA5 - Button Input
RC3:RC0 - LCD I/O D7:D4 (Pins 14:11)
RC4 - LCD E Clocking Pin
RC5 - LCD R/S Pin

myke predko
04.11.08

*/

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);

// 1234567890123456
const char TopMessage[] = " Random Number: ";
const char BotMessage[] = " "; // Number Follows

#define Button RA5 // Define Button Input
#define E RC4 // Define the LCD Control Pins
#define RS RC5

const int Twentyms = 1250; // Declare a Constant for 20 ms Delay
const int Fivems = 300;
const int TwoHundredus = 10;

LCDWrite(int LCDData, int RSValue)
{
int i, j;

PORTC = (LCDData >> 4) & 0x0F; // Get High 4 Bits for Output
RS = RSValue;
E = 1; E = 0; // Toggle the High 4 Bits Out

PORTC = LCDData & 0x0F; // Get Low 4 Bits for Output
RS = RSValue;
E = 1; E = 0; // Toggle the Low 4 Bits Out

if ((0 == (LCDData & 0xFC)) && (0 == RSValue))
j = Fivems; // Set Delay Interval
else
j = TwoHundredus;

for (i = 0; i < j; i++); // Delay for Character

} // End LCDWrite

main()
{
int i, j;

PORTC = 0; // Start with Everything Low
CMCON0 = 7; // Turn off Comparators
ANSEL = 0; // Turn off ADC
TRISC = 0; // All of PORTC are Outputs

OPTION = 0b01001111; // Run TMR0 From PIC MCU Clock

// Initialize LCD according to the Web Page
j = Twentyms;
for (i = 0; i < j; i++); // Wait for LCD to Power Up

PORTC = 3; // Start Initialization Process
E = 1; E = 0; // Send Reset Command
j = Fivems;
for (i = 0; i < j; i++);

E = 1; E = 0; // Repeat Reset Command
j = TwoHundredus;
for (i = 0; i < j; i++);

E = 1; E = 0; // Repeat Reset Command Third Time
j = TwoHundredus;
for (i = 0; i < j; i++);

PORTC = 2; // Initialize LCD 4 Bit Mode
E = 1; E = 0;
j = TwoHundredus;
for (i = 0; i < j; i++);

LCDWrite(0b00101000, 0); // LCD is 4 Bit I/F, 2 Line

LCDWrite(0b00000001, 0); // Clear LCD

LCDWrite(0b00000110, 0); // Move Cursor After Each Character

LCDWrite(0b00001110, 0); // Turn On LCD and Enable Cursor

for (i = 0; TopMessage != 0; i++)
LCDWrite(TopMessage, 1);

while(1 == 1) // Loop Around Waiting for Button
{
i = 0; // Wait 20 ms for Button Up
while (i < Twentyms)
if (0 == Button) // Button Down/Start over
i = 0;
else // Button Up/Increment Count
i = i + 1;
NOP();
i = 0; // Wait 20 ms for Button Down
while (i < Twentyms)
if (1 == Button) // Button Up/Start over
i = 0;
else // Button Down/Increment Count
i = i + 1;

LCDWrite(0b11000000, 0);// Move Cursor to the Second Line

for (i = 0; BotMessage != 0; i++)
LCDWrite(BotMessage, 1);

i = TMR0; // Save TMR0 Value
if (i > 100) // Convert TMR0 Value to Decimal
LCDWrite((i / 100) + '0', 1);
else // No Hundreds to Display
LCDWrite(' ', 1);
if (i > 10)
{
i = i % 100; // Not Greater than 10
LCDWrite((i / 10) + '0', 1);
}
else
LCDWrite(' ', 1);
LCDWrite((i % 10) + '0', 1);

} // elihw
} // End cRandom
 
Top