Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help With Code

Status
Not open for further replies.

j_boodoosingh

New Member
Hey all, i'm new to programming and i'm writing this code for a school project. I have to read the voltage from 2 potentiometers, convert them to angles and display them on an LCD. The code bulids without any errors but i'm concerned about is if this will work. I also have to set predefined angles and display it to the LCD. I need help with the code for setting the angles.To set the angles i have to use the following buttons, one button should be 10, so when this is pressed the displayed angle should increment/decrement by 10, the other button for inc. or dec by 1, a +/- button for selecting if to increment or decrement, x/y select button or seperate buttons for each to select which angle i'm setting, and a button to set the angles (like an offset) from the pots to zero (restart the code). I have no idea how to do this part. The predefined angles go on one line and the calculated angles go on another. Please help this project is due in one week. Ports C and D are not in use.


#include <p18F452.h>
#include <adc.h>
#include <delays.h>
#include <xlcd.h>
//#include <math.h>
#include<stdlib.h>

int result;
//#define read1 0x24;
//#define read2 0x25;

//...........................Delays For LCD..............................
void DelayFor18TCY( void )
{
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
void DelayPORXLCD (void)
{
Delay1KTCYx(60); // Delay of 15ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 16MHz) / 4
// Cycles = 60,000
return;
}
void DelayXLCD (void)
{
Delay1KTCYx(20); // Delay of 5ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 16MHz) / 4
// Cycles = 20,000
return;
}



//...........................AD conversion...............................
int conv(char channel)
{
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_1ANA_0REF, channel & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
}
//......................................................................



void main( void )
{
int x;
int y;
int readx;
int ready;
int sen1; //offset voltage for sensor 1 ,current pos of pot.
int sen2; //offset voltage for sensor 2
int angle1;
int angle2;
char channel;
char a2;

TRISA = 1 ; // Port A is input.
TRISB = 0; // Port B is output.


channel = 'ADC_CH0';
sen1 = conv(channel);//store result from A/D conversion
channel = 'ADC_CH1'
sen2 = conv(channel);



//...........................Continuous Read.............................

for (;;)
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_1ANA_0REF, ADC_CH0 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
readx = result;

if (sen1 > readx)
angle1 = (sen1 - readx)*72;
else
{
angle1 = (readx - sen1)*72;
}


OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_1ANA_0REF, ADC_CH1 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
ready = result;

if (sen2 > ready)
angle2 = (sen2 - ready)*72;
else
{
angle2 = (ready - sen2)*72;
}


{
char data;
//...........................configure external LCD......................
OpenXLCD( FOUR_BIT & LINES_5X7 );
SetDDRamAddr(0x00);
// Set data interface width, # lines, font

// Turn the display on then off
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF);
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON

// Clear display
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(0x01); // Clear display

// Set entry mode inc, no shift
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode

// Set DD Ram address to 0
while(BusyXLCD()); // Wait if LCD busy
SetDDRamAddr(0); // Set Display data ram address to 0


while(1)
{

data = angle1;
while(BusyXLCD()); // Wait if LCD busy
WriteDataXLCD(data); //write to LCD
while(BusyXLCD()); // Wait if LCD busy
SetDDRamAddr(0x05); // Set Display data ram address to 4
while(BusyXLCD());
WriteDataXLCD(angle2); //write to LCD
while(BusyXLCD());

if(data=='Q')
break;
}

}
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top