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.

adc using pic18f45k22 microcontroller

Status
Not open for further replies.

jblonigan

New Member
Hey so I am just beginning with the whole microcontroller programming thing and by piecing some code together from outside sources and putting in what I need the code to do I have this:

C:
#include <htc.h>
#include <stdlib.h>
#include <math.h>



#pragma config FOSC = INTIO67 //     FOSC = INTIO67       Internal oscillator block
#pragma config PLLCFG = ON //     PLLCFG = ON          Oscillator multiplied by 4
#pragma config PRICLKEN = ON //     PRICLKEN = ON        Primary clock enabled
#pragma config FCMEN = OFF //     FCMEN = OFF          Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF //     IESO = OFF           Oscillator Switchover mode disabled
#pragma config PWRTEN = ON //     PWRTEN = ON          Power up timer enabled
#pragma config BOREN = OFF //     BOREN = OFF          Brown-out Reset disabled in hardware and software
#pragma config WDTEN = OFF //     WDTEN = OFF          Watch dog timer is always disabled. SWDTEN has no effect.
#pragma config CCP2MX = PORTB3 //     CCP2MX = PORTB3      CCP2 input/output is multiplexed with RB3
#pragma config PBADEN = OFF //     PBADEN = OFF         PORTB<5:0> pins are configured as digital I/O on Reset
#pragma config HFOFST = ON //     HFOFST = ON          HFINTOSC output and ready status are not delayed by the oscillator stable status
#pragma config T3CMX = PORTB5 //     T3CMX = PORTB5       T3CKI is on RB5
#pragma config P2BMX = PORTC0 //     P2BMX = PORTC0       P2B is on RC0
#pragma config MCLRE = EXTMCLR //     MCLRE = EXTMCLR      MCLR pin enabled, RE3 input pin disabled
#pragma config STVREN = OFF //     STVREN = OFF         Stack full/underflow will not cause Reset
#pragma config LVP = OFF //     LVP = OFF            Single-Supply ICSP disabled
#pragma config XINST = OFF //     XINST = OFF          Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
#pragma config DEBUG = OFF //     DEBUG = OFF          Disabled



void ADCInit(void)

{
	ADCON0 =	0b00000000;
			//0		        Unimplemented
			//-00000--		Analog Channel Select bit,00000=AN0
			//------0-		A/D conversion status bit. 1=in progress
			//-------0		ADC enable bit. 0=ADC disabled. 1=ADC enable

	ADCON1 =	0b00000000;
			//0-------		Trigger select bit, 0=CCP5, 1=CTMU
			//-000----		Unimplemented
			//----00--		A/D VRef+ connected to internal signal
			//------00		A/D Vref- connected to internal signal

	ADCON2 =	0b10000111;
			//1-------		0=left justify, 1=right justify
			//-0------		Unpmplemented
			//--000---		ADC clock source selected to Frc
			//-----111		Clock from internal oscillator

	

}

unsigned int ADCRead(unsigned char ch)

{

	if (ch>13) return 0;		// Invalid channel
	ADCON0 = (ch<<0);		// Select ADC Channel
	ADON=1;				// Switch on the ADC module
	GODONE=1;			// Start conversion
	while(GODONE);			// Wait forthe conversion to finish
		ADON=0;			// Switch off ADC
	return ADRES;			// Return value of the ADC

}

void main(void)

{
	
	TRISB = 0b00000000;		// All B ports are outputs
	

	int ADCRead=0;
	int voltage1=0;
	int voltage2=0;
	int dB=0;
	int i=1;
	/*int doseage [28800];*/
	int j=0;


	while(1)
	{
		ADCRead = ReadADC();
		voltage1= ADCRead/1024;
		voltage2= voltage1-2.5;
		

		if(voltage2<0)
		{
			voltage2=-(voltage2);
		}
		
		dB=log(voltage2/.0003)*123.3 ;

		
		/*if(i=8000000)
		{ 
			doseage[j]=dB;
			i=0;
			j=j+1;
		}
		*/
		
		PORTB |= 0x01; //Turn LED0 on
		PORTB &= (~1x01); //Turn LED0 off
		PORTB &= (~2x01); //Turn LED0 off
		PORTB &= (~3x01); //Turn LED0 off
		PORTB &= (~4x01); //Turn LED0 off
		PORTB &= (~5x01); //Turn LED0 off
		PORTB &= (~6x01); //Turn LED0 off
		
		if(dB>65)
		{
			PORTB |= (1x01); 
			
		}
		
		if(dB>75)
		{
			PORTB |= (2x01);
		}
		
		if(dB>85)
		{
			PORTB |= (3x01);
		}
		
		if(dB>95)
		{
			PORTB |= (4x01);
		}

		if(dB>105)
		{
			PORTB |= (5x01);
		}

		if(dB>115)
		{
			PORTB |= (6x01);
		}
		
		i=i+1;


	}

}

I am taking an input from a microphone and converting it to a decibel value to light up LEDs. I have a feeling there is alot of this I might not need so if someone could help me out I would be extremely grateful.

[MODNOTE]Please use code tags.. symbol '#'[/MODNOTE]
 
Last edited by a moderator:
Code:
PORTB |= 0x01; //Turn LED0 on
		PORTB &= (~1x01); //Turn LED0 off
		PORTB &= (~2x01); //Turn LED0 off
		PORTB &= (~3x01); //Turn LED0 off
		PORTB &= (~4x01); //Turn LED0 off
		PORTB &= (~5x01); //Turn LED0 off
		PORTB &= (~6x01); //Turn LED0 off

I don't get this bit???
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top