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.

PIC Microcontroller PWM and TIMER

Status
Not open for further replies.

torchd

Newbie level 3
Joined
May 7, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,307
Hi guys
I am new to PIC microcontrollers. I am measuring relative humidity in a room by using a resistive humidity sensor. The sensor uses an ac signal. I have generated the ac signal by using the PWM signal from a PIC 4520( 1kHz 0-5V). I send this signal to an op amp and convert the signal to -1 to +1 V square wave. What I need to know is how do I sample the voltage across the sensor and know when my voltage is high(+1V) to be able to sync my ADC samplings.
(I am using CCP1 and Timer 2 )
Thanks in advance

The Code I used to generate the PWM signal is shown below:

#include <htc.h>


void main()
{
int i;
OSCCON = 0b01111110; //oscillator control register set for 8mhz

TRISC=0x00;
T2CON = 0b00000110; // prescaler + turn on TMR2;
PR2 = 0b001111111;
CCP1CON = 0b00111100;
CCPR1L=63;

//TMR2ON=1;
while(1)
{

}


}
 

You can use the timer 2 interrupt.

From data sheet:

When TMR2 is equal to PR2, the following events
occur on the next increment cycle:
• TMR2 is cleared
• The CCPx pin is set.

Timer two interrupt will occur when TMR2 is equal to PR2. This is when the pwm output is set high.
 
  • Like
Reactions: torchd

    torchd

    Points: 2
    Helpful Answer Positive Rating
Hi Guys Thanks for the reply.
After doing some research I found that I cannot use both CCP modules with pwm at different frequencies.( I am using CCP2 for 120kHz pwm signal). So I found that if I want to create a 1kHz square wave using CCP1, will I need to use the compare mode on the CCP1. Can anyone tell how I will be able to read the adc at when pin is high.
Thanks Guys
 

You could use a timer interrupt to generate the square wave.
Pre-load the timer for a 500uS interrupt frequency and toggle an output pin in the interrupt.
Then you would know when the line is high.
 
  • Like
Reactions: torchd

    torchd

    Points: 2
    Helpful Answer Positive Rating
torchd

Why don´t you use TIMER0 and perfor what btbass sugested ?
I didn´t see it being used at above code you posted.

+++
 

Guys I have tried using timer 0 but I only a high signal
Code
Code:
#include <htc.h>
#include <math.h>
__CONFIG(1,XT);
int count=0;
int time=0;
unsigned char shad;

void main(void)
{
	TRISC=0x00;
	T0CON=0b11000111;
	TMR0IE=1;	//Enable TIMER0 Interrupt
	PEIE=1;		//Enable Peripheral Interrupt
	GIE=1;		//Enable INTs globally
	while(1);
	{	RC2=shad;
	}
}





//Main Interrupt Service Routine (ISR)
void interrupt ISR()
{
	//Check if it is TMR0 Overflow ISR
	if(TMR0IE && TMR0IF)
	{
		time++;
		if (time==10)
		{
	
		count++;
	    if (count%2==0)
		{
			shad=0;
		}
		if (count%2==1)
		{
			shad=1;
		}
		if (count==6000)
		{
			count=1;
		}	
		time=0;	
		}//Clear Flag
		TMR0IF=0;
	}
}
 

You can pre load the timer to get the interrupt frequency. You can check this by using Mplab SIM, set the correct oscillator frequency in \Debugger\Settings\, and put a break point in the interrupt routine. Use the stop watch to adjust the pre load value for 500uS interrupt.

Code:
void interrupt ISR()
  {
  static char toggle = 0;

  if(TMR0IF)
    {
    if(!toggle){
     RC2 = 1;
     toggle = 1;
     }
    else{
     RC2 = 0;
     toggle = 0;
     }

    TMR0IF = 0;

    TMR0 = 160;  /* Pre load timer for 500uS interrupt */
    }
  }
 
Last edited:

Couldn't you set that op-amp to generate the needed square wave, and use positive transition as interrupt to start the conversion.
If I even understood the problem correctly.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top