Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is this program doing?

Status
Not open for further replies.

Frisky

Newbie level 1
Joined
May 17, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,290
Dear All,

Could someone explain line by line what this program is doing? Iam new to programming and I am using it as a sample, but don't understand what most of the statements are doing.
Code:
#include <htc.h>
__CONFIG(UNPROTECT & BORDIS & PWRTEN & WDTDIS & INTIO &
MCLREN & FCMDIS & IESODIS);
#define _XTAL_FREQ 8000000
void init(void)
{.
// Configure Ports directions: 1=input, 0=output
TRISD = 0x00;
TRISC = 0xFF;
TRISA = 0xFF;
// 8Mhz Internal Clock
OSCCON = 0b01110000;
// Turn off comparators
CMCON0 = 0x07;
//Turn on ADC
ANSEL = 0b00000001; // select AN0 as an analogue channel (RA0)
ADCON1 = 0b01010000; // select A/D conversation clock Fosc/16
ADCON0 = 0b00000001; // left justified, Vss and Vdd as refs, AN0 is on
// Clear PortD
PORTD = 0x00;
// PWM Setup
PR2 = 0x9A;
CCP2CON = 0b00001100; //capture /compare/pwm module
CCPR2L = 5;
TMR2ON = 1;
}
unsigned int ADCval;
void main(void)
{
init();
RD7 = 1;
while (1){
if(GODONE == 0){
ADCval = ADRESH ;//0-1023 //A/D result high register
ADCval = ADCval >> 2;
CCPR2L = ADCval;
GODONE = 1; // Start the next conversion
}
}
}
 

init function is for initialization of microcontroller. init set the values of ADC and PWM registers to operate them in desired mode. main function calls init in starting and then read the channel RA0 and covert it in to digital.
ADCval >>2 shift right the value of ADCval by 2 digits. What each bit is doing see the user manual for micrcontroller. You will find all these registers by name and their bit information. You can search the register by name (like ADCval) in user manual.
 

which statements, are you doubt?
which statements, you understand /* explain way you know*/
 

looks like voltage controlled PWM to me. PIC reading voltage value through ADC and feed it to PWM, so its duty cycle varies proportional to voltage.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top