Postby Angle » Sun Dec 15, 2019 11:17 pm

Code: Select all
// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
#pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#pragma config LPBOR = OFF // Brown-out Reset Selection bits (BOR disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)

#include <xc.h>
#include <htc.h>
#define _XTAL_FREQ 8000000
#define MINLEVEL 73 //1.42V=>73
#define MAXLEVEL 130 //2.54V=>130
void InitPWM();
void ADCInit();
unsigned char ADCRead();
void Interrupt();
unsigned char advalue;
unsigned char PWM;
void __interrupt() interrup_handler(void)
         PIR1bits.ADIF = 0;

void main()

PORTA = 0b00000000;
TRISA = 0b00000100;
ANSELA = 0b00000100;
CLC1CON = 0b00000000;
CWG1CON0 = 0b00000000;

INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
PIE1bits.ADIE = 1;

ADCON=0b00101001; //Fosc/8, AN2 setted as analog input,enable ADC

void InitPWM()
PWM1CON = 0b00000000; // Clear PWM1CON
PR2 = 77; //400Hz=77
PWM1CON = 0b11000000; //Enable PWM Module, Module Output

TMR2IF = 0; //Clear the timer 2 interrupt flag
T2CON = 0b00000011; //Set prescaler to 1

TMR2ON = 1; //Enable timer 2
TRISA=0b00000100; //Enable the pwm pin output

//Set duty cycle to %70
PWM1DCH = 0b00110110 ;
PWM1DCL = 0b10000000;


//Function to Initialise the ADC Module
void ADCInit()
//Port Configuration
PIE1bits.ADIE = 1;

//Function to Read ADC channel
unsigned char ADCRead()
GO_nDONE = 1;
while (GO_nDONE); // wait until conversion complete
return ADRES; //return 8 bit value*/

It's a simple thing I want to do, but I just couldn't do it. I'm new to MCU.
I want to do
1-)As long as the voltage I read from the RA2 pin is between 1.42V and 2.54V, the output of the RA0 pin is 400Hz with 70% Duty Cyle.
2-)If the voltage at the RA2 pin goes below 1.42V or above 2.54V while PWM is running, it will not output PWM from the RA0 pin.
3-)If the voltage on the RA2 pin is between 1.42V and 2.54V, it will continue to give PWM again.
4-)This will last forever as long as the MCU is running.

I can not. There's a mistake somewhere. It doesn't work the way I want.
1-)I get the PWM output correctly. No problem creating PWM.
2-)I can read at ADC value.
3-)But when I read the voltage from the ADC I want it to come out of PWM, but PWM continues to work.So I can't control the ADC change.
Posts: 1
Joined: Sun Dec 15, 2019 11:11 pm

Return to ADC & Comparators

Who is online

Users browsing this forum: No registered users and 1 guest