• AVR Freaks

Helpful ReplyHot!PIC10F320 ADC Value Reading

Page: 12 > Showing page 1 of 2
Author
Angle
New Member
  • Total Posts : 23
  • 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.
 
 
 
post edited by Angle - 2020/01/31 09:11:31
#1
1and0
Access is Denied
  • Total Posts : 11167
  • 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 : 11167
  • 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 : 23
  • 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.



post edited by Angle - 2020/01/31 09:04:26
#4
Angle
New Member
  • Total Posts : 23
  • 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. 
 
post edited by Angle - 2020/01/31 09:09:21
#5
pcbbc
Super Member
  • Total Posts : 1703
  • 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 : 11167
  • 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 : 23
  • 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
Yes
post edited by Angle - 2020/01/31 09:05:25
#8
Angle
New Member
  • Total Posts : 23
  • 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.


post edited by Angle - 2020/01/31 09:12:07
#9
1and0
Access is Denied
  • Total Posts : 11167
  • 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 : 11167
  • 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 : 23
  • 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.
 
post edited by Angle - 2020/01/31 09:09:56
#12
Angle
New Member
  • Total Posts : 23
  • 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 : 1797
  • 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 : 23
  • 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
Yes
post edited by Angle - 2020/01/31 09:10:27
#15
Angle
New Member
  • Total Posts : 23
  • 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 
post edited by Angle - 2020/01/31 09:15:49
#16
ric
Super Member
  • Total Posts : 28435
  • 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 : 23
  • 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
yes
post edited by Angle - 2020/01/31 09:11:07
#18
ric
Super Member
  • Total Posts : 28435
  • 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 : 23
  • 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.
 
post edited by Angle - 2020/01/31 09:08:55
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5