Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am doing a project about computer based oscilloscope. I want to program a 16F877A pic for get analog input and convert that input to digital value and send that value via a serial port for plotting. Please help me for program the pic. I am using MikroC (C language). I don't know how to manage USART & ADC methods in mikroC.

Please.....Please.... Help me quickly.....
Posted

/// Posted by : wasio_707@yahoo.com

http://www.engineersgarage.com/embedded/pic-microcontroller-projects/adc-circuit


// ADC


// Program to depict working with inbuilt ADC of PIC18F4550 Microcontroller
// This code uses Channel0 (zero) of PIC's ADC Module

// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L, // Divide clock by 2
_FOSC_HS_1H, // Select High Speed (HS) oscillator
_WDT_OFF_2H, // Watchdog Timer off
MCLRE_ON_3H // Master Clear on
*/

#define rs LATA.F0
#define rw LATA.F1
#define en LATA.F2
#define lcdport LATB

void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void adc_con(unsigned int);
void adc_init();

unsigned char data[20]="ADC OUTPUT=";
unsigned int digital_out[10],avg_output=0,temp;
unsigned int i=0;


void main()
{
TRISA=0x01; // Configure RA0 as input pin
LATA=0;
TRISB=0; // Configure Port B as output port
LATB=0;
TRISD=0;
LATD=0;
lcd_ini(); // LCD initialization
while(data[i]!='\0')
{
lcddata(data[i]); // Call lcddata function to send character one by from 'data' array
i++;
}

adc_init(); //ADC Initialization

while(1)
{
temp=0;
for(i=0;i<10;i++)
{
ADCON0|=(1<<GO); // Start A/D conversion
while(!(ADCON0 & (1<<GO))); // Wait until conversion gets over
digital_out[i]=((ADRESL)|(ADRESH<<8)); // Store 10-bit output into a 16-bit variable
Delay_ms(20);
temp=temp+digital_out[i];
}
avg_output=temp/10; // Take average of ten digital values for stablity
adc_con(avg_output); // Function to convert the decimal vaule to its corresponding ASCII

}
}


void adc_init()
{
ADCON1=0x0E; // Make RA0/AN0 pin as analog pin (Other pins remain to be digital I/O)
ADCON0=0x00; // Select Channel0 & ADC off
ADCON2=0x8A; // Left justified, 2TAD acquiciation time, Fosc/32 clock option
ADCON0.ADON=1; // Enable ADC
}


void lcd_ini()
{
lcdcmd(0x38); // Configure the LCD in 8-bit mode, 2 line and 5x7 font
lcdcmd(0x0C); // Display On and Cursor Off
lcdcmd(0x01); // Clear display screen
lcdcmd(0x06); // Increment cursor
lcdcmd(0x80); // Set cursor position to 1st line, 1st column
}


void adc_con(unsigned int adc_out)
{
unsigned int adc_out1;
int i=0;
char position=0xC3;

for(i=0;i<=3;i++)
{
adc_out1=adc_out%10; // To exract the unit position digit
adc_out=adc_out/10;
lcdcmd(position);
lcddata(48+adc_out1); // Convert into its corresponding ASCII
position--;

}
}


void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
Delay_ms(10);
en=0;
}


void lcddata(unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
Delay_ms(10);
en=0;
}
 
Share this answer
 
Comments
shafiq2eng 31-Jan-14 12:16pm    
i mad Voltage meter Using PIC MikroC successfully and now i want to send this analogue data on serial port to show voltage on Visual Studio Form, Pleas help me....
My codes are below

sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D4 at RC2_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D6 at RC4_bit;
sbit LCD_D7 at RC5_bit;

sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC2_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISC5_bit;
//************************************************//

void main() {
char ch;
int adc_read_val;
long tlong;
ANSEL = 0b00000001; // Configure AN pins as digital I/O
/*ADCON0= 0b10000011;
ADCON1=0b00000000;*/
vcfg_bit=0;
// CM0_bit=0;CM1_bit=0;CM2_bit=0;
TRISA=1;
PORTA=0;
Lcd_Init();
Lcd_Cmd(_Lcd_clear);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_out(1,1,"VOLT METER");
Delay_ms(1000);
//************************************************//
while(1){//start of while
adc_read_val=adc_read(0);
tlong=(long)adc_read_val*5000;
tlong=tlong/1023;
ch=tlong/1000;
Lcd_Chr(2,3,ch+48);
lcd_chr_cp('.');
ch=(tlong/100)%10;
lcd_chr_cp(48+ch);
ch=(tlong/10)%10;
lcd_chr_cp(48+ch);
ch=tlong%10;
lcd_chr_cp(48+ch);
lcd_chr_cp('V');
}//end of while
}
Go to the PIC website - they have sample code and are pretty helpful generally.

PIC is a bit specialist for a general site like this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900