View previous topic :: View next topic |
Author |
Message |
amarquez
Joined: 24 Jun 2009 Posts: 11
|
Malfunctions in PIC10F322 |
Posted: Thu Jul 19, 2012 2:18 am |
|
|
Hello everyone!
I am using the PIC10F322, and compiler version is 4.135.
I have to generate a PWM, but has not implemented the hardware driver for PWM. So, I tried to generate PWM with timer, and I checked the timer1, timer0 and WDT don't work.
Can anyone help me? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Jul 19, 2012 6:34 am |
|
|
Give us a small example of what you have tried. Make it a short complete compilable program. Tell us what you expect it to do and what it actually does. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
amarquez
Joined: 24 Jun 2009 Posts: 11
|
|
Posted: Thu Jul 19, 2012 6:59 am |
|
|
The hardware PWM is not implemented for the PIC10F322.
I'm trying to generate PWM with timer2, I checked out PIN_A1, and no answer.
Code: |
#include <10F322.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC //Internal RC Osc
#FUSES NOBROWNOUT //No brownout reset
#FUSES WDT_SW //Watch Dog Timer, software
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES PUT
#use delay(int=8000000)
int16 cont=0;
int16 freq=10; // PWM freq
int16 width=5; // PWM width
#INT_TIMER2
void TIMER2_isr(void) {
contador++;
if (cont<=width){
output_high(PIN_A1); // DT
}else{
output_low(PIN_A1);
}
if (cont==freq) {
cont=0;
}
}
//!#INT_TIMER0
//!void TIMER0_isr(void) {
//! output_low(PIN_A2);
//!}
void main() {
//! setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64|RTCC_8_bit); //3.2 ms overflow
setup_timer_2(T2_DIV_BY_16,19,16); //64.0 us overflow, 1.0 ms interrupt
//! enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
set_tris_a(0b1001);
output_high(PIN_A2);
while(TRUE){
}
}
|
|
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Thu Jul 19, 2012 7:09 am |
|
|
Maybe I'm still asleep (entirely possible), but where does "cont" get incremented ?
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19482
|
|
Posted: Thu Jul 19, 2012 7:44 am |
|
|
First thing, post code that you are actually trying. What you have posted, won't compile (contador is not declared anywhere to start).
Second, you talk about trying timer1. The chip does not _have_ a timer1....
Then your calculation for the timer2 period is 'wrong'.
You have:
Master clock/4/16/20/16 = 390.625Hz interrupt.
To get 1mSec, you need master clock/8000. Given the fixed/4 division, you need something like:
setup_timer_2(T2_DIV_BY_1,199,10);
Then, prove you can access the pin. Toggle it high and low, without using the interrupt at all. On this pin, the NCO module, and the clock reference output, both have higher priority than basic 'port data'. You need to ensure these are off, before you can access the pin.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 19, 2012 5:32 pm |
|
|
Here's a PWM program that you can try. I don't have a 10F320 or a
10F322, so it's not tested. You should see a signal on pin A0. If it doesn't
work, you'll have to wait until I can get one of those PICs to test.
I used vs. 4.135 to develop this code.
Code: |
#include <10F322.H>
#fuses INTRC,NOWDT,BROWNOUT,PUT,NOMCLR
#use delay(clock=4M)
#define PWM1_PIN PIN_A0
#define PWM2_PIN PIN_A1
#define PWM_ENABLED 0x80
#define PWM_DISABLED 0
#define PWM_OUTPUT 0x40
#define PWM_ACTIVE_LOW 0x10
#define PWM_ACTIVE_HIGH 0
#byte TMR2 = 0x11
#byte PR2 = 0x12
#byte T2CON = 0x13
#byte PWM1DCL = 0x14
#byte PWM1DC = 0x15
#byte PWM1CON = 0x16
//----------------------------------------------------
// This macro provides the setup_pwm1() function.
#define setup_pwm1(mode) \
output_low(PWM1_PIN); \
PWM1CON = mode;
//----------------------------------------------------
// This macro provides the set_pwm1_duty() function.
#define set_pwm1_duty(duty) \
if(duty > 255L) \
{PWM1DC = (duty >> 2); PWM1DCL = (duty << 6); } \
else \
{PWM1DC = duty; PWM1DCL = 0;}
//----------------------------------------------------
// This macro replaces the CCS built-in function setup_timer_2():
#define setup_timer_2(t2_mode, t2_period, t2_ps) \
T2CON = (t2_mode | ((t2_ps -1) << 3)); \
PR2 = (t2_period);
//=======================================
void main()
{
setup_pwm1(PWM_ENABLED | PWM_OUTPUT | PWM_ACTIVE_HIGH);
setup_timer_2(T2_DIV_BY_1, 249, 1); // 4 KHz PWM frequency
set_pwm1_duty(500L); // 50% duty cycle in 10-bit PWM mode
while(1);
} |
|
|
|
amarquez
Joined: 24 Jun 2009 Posts: 11
|
|
Posted: Fri Jul 20, 2012 2:32 am |
|
|
Thanks for your help,
I checked the PWM. I've tried in the PIC10F32x, and have it working.
I can not change the timer2 frequency to operate between 300Hz and 600Hz.
Could you tell me how to operate the timer2 between 300Hz and 600 Hz? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|