• AVR Freaks

Helpful ReplyHot!PIC10F320 ADC Value Reading

Page: 12 > Showing page 1 of 2
Author
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
2019/12/10 22:18:20 (permalink)
0

PIC10F320 ADC Value Reading

I use the PIC10F320 processor in my project.
In my project, I read the analog value from the RA2 pin. Analog value is between 0-5V. If the analog value is between 1.2V-2.5V, I want to output the PWM from the RA0 pin. I can output PWM, but I can't read the analog value. Can you help me with this?
I share the codes of my project.
 

// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#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;
void main()
{

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

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

while(1)
{
ADCRead ();
advalue=ADRES; // AD conversion result
if (MINLEVEL<advalue<MAXLEVEL)
{
continue;
}
else
{
break;
}
}
}
void InitPWM()
{

PWM1CON = 0b00000000; // Clear PWM1CON
PR2 = 0b01001101; // Configure the Timer2 period - decimal 400Hz=77.125
// 400Hz=77 decimal=01001101
PWM1CON = 0b11000000; //Enable PWM Module, Module Output

PWM1DCH = 0b00000000; // Clear duty cycle registers
PWM1DCL = 0b00000000;


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

TMR2ON = 1; //Enable timer 2


TRISA=0b00000000; //Enable the pwm pin output

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


return;
}
//Function to Initialise the ADC Module
void ADCInit()
{
//Port Configuration
ADCON=0b00101010;

}
//Function to Read ADC channel
unsigned char ADCRead()
{
GO_nDONE = 1;
while (GO_nDONE); // wait until conversion complete
return ADRES; //return 8 bit value*/
}
#1
1and0
Access is Denied
  • Total Posts : 10221
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 04:24:20 (permalink)
0
This is your main() function:
void main()
{
    // snipped
    while(1)
    {
        ADCRead ();
        advalue=ADRES; // AD conversion result
        if (MINLEVEL<advalue<MAXLEVEL)
        {
            continue;
        }
        else
        {
            break;
        }
    }
}

What do you expect it to do?  By the way, that if() statement is NOT doing what you think!  Also, ADCRead() returns a value, so use that instead.
#2
1and0
Access is Denied
  • Total Posts : 10221
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 04:56:46 (permalink)
+2 (2)
There is no need to clear a register before it is assigned a value, so these are redundant:
    PWM1CON = 0b00000000; // Clear PWM1CON
   
    PWM1DCH = 0b00000000; // Clear duty cycle registers
    PWM1DCL = 0b00000000;

 
The compiler understands decimal numbers too; i.e. this
    PR2 = 0b01001101; // Configure the Timer2 period - decimal 400Hz=77.125
    // 400Hz=77 decimal=01001101

is more readable as this
    PR2 = 77;

 
Also, this
    TRISA=0b00000000; //Enable the pwm pin output

configures ALL pins to OUTPUT.  And your ADCInit() function is unused.
#3
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 06:00:48 (permalink)
0
Thank you for your reply. I made the necessary corrections. But still ADC reading does not work.

// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#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;

void main()
{

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

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

while(1)
{
ADCRead ();
advalue=ADRES; // AD conversion result
if (MINLEVEL<advalue<MAXLEVEL)
{
continue;
}
else
{
break;
}
}
}
void InitPWM()
{

PWM1CON = 0b00000000; // Clear PWM1CON
PR2 = 77.125; //400Hz=77.125
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;
return;
}

//Function to Initialise the ADC Module
void ADCInit()
{
//Port Configuration
ADCON=0b00101010;

}
//Function to Read ADC channel
unsigned char ADCRead()
{
GO_nDONE = 1;
while (GO_nDONE); // wait until conversion complete
return ADRES; //return 8 bit value*/
}
#4
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 06:08:14 (permalink)
0
Exactly what I want to do. Continuously read analogue value from pin RA2. If the value I read from the RA2 pin is between 1.2V and 2.5V, output the PWM from the RA0 pin. If the value I read from RA2 pin is not between 1.2V and 2.5V, PWM output will not be output from RA0 pin. The device must continuously check the analog value while it is operating. The PWM output is running but cannot read and evaluate the analog value at pin RA2.
 
#5
pcbbc
Super Member
  • Total Posts : 1445
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 07:12:43 (permalink)
+3 (3)
You've not corrected this (1and0 post #2)...
ADCRead ();
advalue=ADRES; // AD conversion result

..to...
advalue=ADCRead ();

 
This line is still wrong (1and0 post #2)...
if (MINLEVEL<advalue<MAXLEVEL)

...read up on how to write expressions (in just about every programming langauge BTW, not just C).
 
else
{
break;
}

Where, exactly, are you expecting the main while loop to break to????
 
The decimals are pointless (1and0 post #3)...
PR2 = 77.125; //400Hz=77.125

You can only set an integer value to a register.  So 77 is sufficient.
 
Please, learn how to place [​code]//code tags[​/code] around your code before posting.
#6
1and0
Access is Denied
  • Total Posts : 10221
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 08:01:36 (permalink)
+3 (3)
Freedom_Angle
Exactly what I want to do. Continuously read analogue value from pin RA2.

Okay.
 

If the value I read from the RA2 pin is between 1.2V and 2.5V, output the PWM from the RA0 pin.

Okay. But you will have to start it again after it stops.
 

If the value I read from RA2 pin is not between 1.2V and 2.5V, PWM output will not be output from RA0 pin.

You're not stopping the PWM. It "break" to where?
 

The device must continuously check the analog value while it is operating. The PWM output is running but cannot read and evaluate the analog value at pin RA2.

It reads the analog value continuously, but it is NOT using the value correctly. As said, read up on how to write expressions.
 
The way you have written the if() statement, the compiler will remove all the code for both if() and else() because the condition is always true.
#7
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 08:24:42 (permalink)
0
// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#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;

void main()
{

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

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

while(1)
{
advalue=ADCRead (); // AD conversion result
if (MINLEVEL<advalue<MAXLEVEL)
{
continue;
}
else
{
break;
}
}
}
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;
return;
}

//Function to Initialise the ADC Module
void ADCInit()
{
//Port Configuration
ADCON=0b00101010;

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

#8
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 08:35:42 (permalink)
0
Okay. But you will have to start it again after it stops.
I would like to constantly check the analog value with the while loop.
 
You're not stopping the PWM. It "break" to where?
If the incoming analog value is not between 1.2V and 2.5V, it will exit the loop and will not output PWM.
 
It reads the analog value continuously, but it is NOT using the value correctly. As said, read up on how to write expressions.The way you have written the if() statement, the compiler will remove all the code for both if() and else() because the condition is always true.
 
True, always sees this true. I'm searching how it should be.
#9
1and0
Access is Denied
  • Total Posts : 10221
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 08:48:52 (permalink)
+1 (1)
Freedom_Angle
I would like to constantly check the analog value with the while loop.

Your code does that.
 

If the incoming analog value is not between 1.2V and 2.5V, it will exit the loop and will not output PWM.

Yes, it will exit the while() loop, fall off the end of main(), and then reset and execute main() again. Again, it is not stopping the PWM.
 

True, always sees this true. I'm searching how it should be.

As said several times already, read up on how to write C expressions.
post edited by 1and0 - 2019/12/11 08:50:41
#10
1and0
Access is Denied
  • Total Posts : 10221
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 09:05:32 (permalink) ☄ Helpfulby Angle 2019/12/11 14:15:41
+3 (3)
Here's pseudo code of what your main loop should do:
 
1. Read ADC value.
2. If value > minimum AND value < maximum, start/enable PWM.
3. Otherwise, stop/disable PWM.
4. Go back to Step 1.
 
Now, try to code that.
#11
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 14:04:47 (permalink)
0
The problem was partially solved. But there is a problem.
 
If the value I read from RA2 pin is between 1.2V and 2.5V, I get PWM output from RA0 pin.OKEY
If the value I read from the RA2 pin is not between 1.2V and 2.5V, I don't get PWM output from the RA0 pin.OKEY
BUT
When the analog value goes below 1.2V and above 2.5V, it still outputs PWM.
 

// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#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;

void main()
{

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

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



while(1)
{
ADCRead ();
if ((MINLEVEL<ADRES)&(ADRES<MAXLEVEL))
{
InitPWM();
}
else
{
PORTA = 0b00000000;
}
}
}

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;

return;
}

//Function to Initialise the ADC Module
void ADCInit()
{
//Port Configuration
ADCON=0b00101010;

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

 
post edited by Freedom_Angle - 2019/12/11 14:07:53
#12
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 14:13:48 (permalink)
0
I need to constantly check the analogue value.
But I can't control it all the time.
The program checks once while it is running, then does not evaluate the analog value.
How do I do that.
 
I need to constantly monitor the ADC value with interrupt. But I don't know how.
post edited by Angle - 2019/12/11 14:30:58
#13
mbrowning
USNA79
  • Total Posts : 1583
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 14:29:33 (permalink) ☄ Helpfulby Angle 2019/12/11 14:45:05
+2 (2)
if ((MINLEVEL<ADRES)  (ADRES<MAXLEVEL)) { << you want logical and, not bitwise and
    InitPWM();
} else {
    PORTA = 0b00000000;   << this does nothing since the PWM module takes over the pins.
}

Read the PWMx Pin Configuration section of the datasheet (section 18.1 in the old version I have). It tells you how to get PWM to relinquish the output pin.
#14
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 14:44:22 (permalink)
0

// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#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 LEVEL9 73 //1.43V=>73
#define LEVEL10 81 //1.59V=>81
#define LEVEL14 113 //2.22V=>113
#define LEVEL16 130 //2.54V=>130

void InitPWM1();
void InitPWM2();
void InitPWM3();
void InitPWM4();
void ADCInit();

unsigned char ADCRead();
void Interrupt();

void main()
{

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

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


while(1)
{
ADCRead();
if ((LEVEL9<=ADRES)&&(ADRES<=LEVEL16))
{
InitPWM2();
}
else
{
PWM1CON = 0b00000000;
PORTA = 0b00000000;
}
}
}

void InitPWM1()
{
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 80
PWM1DCH = 0b00110010;
PWM1DCL = 0b10000000;

return;
}

void InitPWM2()
{
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;

return;
}

void InitPWM3()
{
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 60
PWM1DCH = 0b00100101;
PWM1DCL = 0b11000000;

return;
}

//Function to Initialise the ADC Module
void ADCInit()
{
//Port Configuration
ADCON=0b00101010;

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

post edited by Angle - 2019/12/11 17:10:14
#15
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/11 17:12:37 (permalink)
0

I can check the analog value read from the RA2 pin.
But it doesn't work well. For example, if the Analog value is not in the range I want, I want the PWM output and the program to do nothing.
But PWM does not output, but logic 5V output.
post edited by Angle - 2019/12/11 17:13:42
#16
ric
Super Member
  • Total Posts : 25214
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC10F320 ADC Value Reading 2019/12/11 17:53:49 (permalink)
+2 (2)
The image you tried to embed is actually a web page, which is why it won't display here.
The link is https://ibb.co/7b9YfT4
 
Are you testing this on real hardware, or in some sort of simulator?
 
You keep ignoring people telling you how to use the ADCRead function correctly.
 
Your main loop needs to be a bit more intelligent. If the PWM is already on, and your test confirms that it should stay on, then do NOT reinitialise it.
As it stands, while the signal is in range, you are rapidly reinitialising the PWM over and over, rather than allowing it to run.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/15 11:31:45 (permalink)
0
// PIC10F320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#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)
{
       if(ADIF){
       ADCInit();
       ADCRead();
       if ((MINLEVEL>ADRES)||(ADRES>MAXLEVEL))
        {
           RA0=0;
        }
        
       }
         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

ADCInit();
ADCRead();
InitPWM();
while(1)
{
    
       
    
}
}
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;

return;
}

//Function to Initialise the ADC Module
void ADCInit()
{
//Port Configuration
ADCON=0b00101010;
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*/
}

 
I still can't get it to work. Can you help me?
#18
ric
Super Member
  • Total Posts : 25214
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC10F320 ADC Value Reading 2019/12/15 12:41:46 (permalink)
+2 (2)
There's multiple things wrong with this code.
You really gained nothing by trying to do it all inside an interrupt service, and it simply won't work as written.
 
If you are going to use an ADIF interrupt service, the interrupt will occur when the ADC peripheral has just finished measuring a sample.
That means it is insane to then reinitialise the ADC peripheral, manually trigger a conversion, and wait for it to complete, all inside the interrupt service.
 
IF you are going to use an interrupt, then the FIRST ting you would do is read the value that has just been measured.
THEN you would restart the next conversion (by setting the GO bit), which will trigger another interrupt when that conversion is complete.
Your will trigger a single interrupt, then it will never interrupt again.
 
Also, in this interrupt service, you raise the RA0 pin when your conditions are not met, but you never do anything else.
 
Personally, I think you have just confused yourself by trying to use an interrupt. Throw that away and try to get something working without interrupts first.
Then, just think through clearly what your code needs to do.
You need to keep track of if PWM is off or on. If it's off, and you need it on, THEN you initialise PWM.
If it's already on, and you need it on, you do nothing (let it run)
If it's on, and you need it off, you need to turn it off
If it's off, and you need it off, you do nothing.
That's logically all very simple. You just need to write the code to do it. Don't look for shortcuts because none of yours work.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#19
Angle
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/12/07 14:31:55
  • Location: 0
  • Status: offline
Re: PIC10F320 ADC Value Reading 2019/12/15 14:56:13 (permalink)
0
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.
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5