Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
02/09/05 14:50
Read: times


 
#86988 - 8051 MicroC Voltage Meter--Please Help
We have recently been assigned to create a voltage meter on a BiPom MiniMax/51 Board which takes in analog voltage waves and returns it in digital format on an LCD screen. So far we have written most of the code (ADC converter, LCD support) but are stuck on how exactly we can send the digital data to to the LCD. Our teacher has suggested using arrays. I have posted the code please help.

#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>
#define CLK P1.6
#define CS P3.7
#define DI P1.4
#define DO P1
#define RS P0.0 // LCD register select bit
#define RW P0.1 // LCD read/write bit
#define EN P0.2 // LCD enable bit
#define ROWNUM 4 // # of rows in keypad
#define COLNUM 3 // # of columns in keypad

//function prototypes
void clock(void);
unsigned char convert(unsigned char channel);
unsigned char scankeypad(void); // Keypad function
void Init_CPU(void);
void Initial_LCD(void); // Initialization function
void EN_low(void); //Set EN to low position function
void LCD_putc(unsigned char key); // Display single character function
void LCD_puts(const unsigned char * str);// Display character string function


static char KeyTable[]={'1','2','3', // Output of buttons pressed on keypad
'4','5','6',
` '7','8','9',
'*','0','#'};
static unsigned char RowTable[]={0xFE,0xFD,0xFB,0xF7}; // addresses of rows

//main starts here
void main()
{
unsigned char RETURN;
static char Voltage10[]={'1','2','3','4','5','6','7','8','9'};
Initial_LCD(); // Run function Initial_LCD
Init_CPU(); //do all initializations
while(1)
{
RETURN=convert(0);
LCD_putc(Voltage10(RETURN));
printf("channel 0 = %dn",convert(0));
printf("channel 1 = %dn",convert(1)); // print converted readout of channel 1
printf("channel 2 = %dn",convert(2)); // print converted readout of channel 2
printf("channel 3 = %dn",convert(3)); // print converted readout of channel 3
}
}//end main
//define Init_CPU()
void Init_CPU(void)
{
serinit(9600); //initialize serial port
clrbit(CS); //CS is output to ADC
clrbit(CLK); //CLK is clock to ADC
} //end of Init_CPU()

//define function clock
void clock(void)
{
setbit(CLK);
clrbit(CLK);
}

//define function convert
unsigned char convert(unsigned char channel)
{
//variable declaration
unsigned char i, value;
unsigned char option;

//before first clock initial state
clrbit(CLK);
setbit(CS);
clrbit(DI);

//1st clock
clrbit(CS);
setbit(DI);
clock();
delay(1);

//2nd clock
setbit(DI);
clock();
delay(1);

//3rd and 4th clocks
option=channel;
switch(option)
{
case 0: //channel 0
clrbit(DI); //3rd clock
clock();
delay(1);
clrbit(DI); //4th clock
clock();
delay(1);
break;

case 1: //channel 1
setbit(DI); //3rd clock
clock();
delay(1);
clrbit(DI); //4th clock
clock();
delay(1);
break;

case 2: //channel 2
setbit(DI); //3rd clock
clock();
delay(1);
clrbit(DI); //4th clock
clock();
delay(1);
break;

case 3: //channel 3
setbit(DI); //3rd clock
clock();
delay(1);
clrbit(DI); //4th clock
clock();
delay(1);
break;
} //end switch

setbit(DI); //DI is input
clock(); //5th clock
delay(1); //wait for MUX to settle

//start clocking in to 8051 the converted digital calue from MSB
value=0; //00000000 Binary
for(i=0; i<8; i++) //clock 6, to clock 13
{
if(DO&0x10)
value=value|1;
clock();
if(i<7)
value=value<<1;
} //end for

for(i=0; i<8; i++)
{
clock();
}//end of for
return value;
} //end convert()

void Initial_LCD(void) // Function initializes LCD
{
P0=0; // Make all bits on port 0 output bits
delay(40); // 20 ms delay
P0=0x34; // Block 1
EN_low();
delay(10);
P0=0x34; // Block 2
EN_low();
P0=0x34; // Block 3
EN_low();
P0=0x24; // Block 4
EN_low();
P0=0x24; // Block 5
EN_low();
P0=0x64; // Block 6
EN_low();
P0=0x04; // Block 7
EN_low();
P0=0x84; // Block 8
EN_low();
P0=0x04; // Block 9
EN_low();
P0=0x14; // Block 10
EN_low();
P0=0x04; // Block 11
EN_low();
P0=0x64; // Block 12
EN_low();
P0=0x04; // Block 13
EN_low();
P0=0xE4; // Block 14
EN_low();
}

void EN_low(void) // Set EN to low position, done with procedure
{
delay(1); // Create 0.5 ms delay
clrbit(EN); // set EN to low position
delay(5); // Create 2.5 ms delay
}

void LCD_putc(unsigned char key)
{
unsigned char value1, value2; // Create variables value1, value2
value1=key&0xF0; // Use OR function to separate low nibble out
value1=value1|0x05; // Use AND function to make low nibble 5
P0=value1; // Send out high nibble
EN_low();
value2=key&0x0F; // Use OR function to separate high nibble out
value2=value2<<4; // Shift low nibble bits to high nibble position
value2=value2|0x05; // Use AND function to make low nibble 5
P0=value2; // Send out low nibble
EN_low();
}

void LCD_puts(const unsigned char * str)
{
register unsigned char char1; // Create registered variable char1
while ((char1=* str++)) // While next character in the string is not null …
{
LCD_putc(char1); // Run function LCD_putc with input "char1"
}
}

unsigned char scankeypad(void)
{
unsigned char row, col; // Declare variables row and col
col=0; // set col equal to 0
for(row=0; row<ROWNUM; row++) // Create constant loop that changes rows
{
P2=RowTable[row]; // set P2 equal to row number's port address
if(!(P2&0x10))
col=1;
if(!(P2&0x20))
col=2;
if(!(P2&0x40))
col=3;

if(col!=0) // if col is not equal to 0
{
return (col-1+COLNUM*row); // return pressed key information…
delay(100);
} // End of if loop
} // End of for loop
} // End of function

List of 12 messages in thread
TopicAuthorDate
8051 MicroC Voltage Meter--Please Help            01/01/70 00:00      
   woefully incomplete and a suggestion            01/01/70 00:00      
   congratulations            01/01/70 00:00      
   Real Code?            01/01/70 00:00      
   code compiles fine in MicroIDE            01/01/70 00:00      
      The Development Process            01/01/70 00:00      
      and so what            01/01/70 00:00      
      Code?            01/01/70 00:00      
      A couple of observations            01/01/70 00:00      
   almost there            01/01/70 00:00      
      absolutely            01/01/70 00:00      
         Sending data to the lcd            01/01/70 00:00      

Back to Subject List