Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 25, 2024, 10:30:24 10:30


Login with username, password and session length


Pages: [1]
Print
Author Topic: Software PWM  (Read 7407 times)
0 Members and 2 Guests are viewing this topic.
king
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 2
-Receive: 21


Jack of All But Master of One


« on: December 18, 2009, 09:46:52 09:46 »

I have generated Software PWM of 20Khz by the help of Timer1 and it is working fine but the problem which i am facing now is that ,the main body is executing just one time only. I have attached the source code as well so please read my source code and please tell me what the hell is happening in code that main body has been executed just one time.

device=16f876a
xtal=20
DECLARE CCP1_PIN PORTc.2   
PIE1 = $01          'Enable TMR1 overflow interrupt
INTCON=%11010000    'Enable Global Interrupt,Peripheral and External Interrupts
all_digital=true
ON_INTERRUPT GOTO SENSOR_INT
dim TIMER1 as tmr1l.word
DIM A AS WORD
dim B as word
Symbol T1CKPS0 = T1CON.4    ' Timer1 Input Clock Prescale Select bits
Symbol T1CKPS1 = T1CON.5    ' Timer1 Input Clock Prescale Select bits
syMbol TMR1ON = T1CON.0     ' START OR STOP TIMER1
SYMBOL FTMR1 = PIr1.0       ' TIMER1 OVERFLOW FLAG

T1CKPS0=0      'PRE SCALAR SET TO 8
T1CKPS1=0      '
TIMER1=65335'485    'TIMER OFFSET FOR TIME DELAY=100ms
low portc.4
low porta.3
TMR1ON=0'1
ftmr1=0
B=1
Pie1.1=0       'when this value=0 then main body execute and if it is=1 then main execute ONLY ONE TIME
hpwm 1,200,20000
GOTO MAIN

'********************************************************
main:
toggle porta.3
HIGH 7

INC B
for a=1 to  65535
NOP
nop
nop
nop
nop
next

for a=1 to  65535
NOP
nop
nop
nop
nop
next

goto main

'**********************START ISR( INTERRUPT SERVICE ROUTINE )*****************
SENSOR_INT:
if pir1.1=1 then
TIMER1=65335'485
HIGH PORTC.4
TMR1ON=1
PIR1.1=0
ENDIF

IF FTMR1=1 THEN
LOW PORTC.4
FTMR1=0
ENDIF
retfie fast
'tmr1on=0
'timer1=65410
'toggle portc.4
'ftmr1=0
'tmr1on=1
'CONTEXT RESTORE
'************************END ISR( INTERRUPT SERVICE ROUTINE)*****************


Also if i have change the value of PIE1.1=1 to PIE1.1=0 then software pwm stops and main body executes normally.

I hope someone can help me.

Regards

King
Logged
pickit2
Moderator
Hero Member
*****
Offline Offline

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #1 on: December 18, 2009, 12:13:15 12:13 »

a quick look at code  
PIE1 $01 is wrong, you need to set more bits not just %00000001
PIE1 = %00000101    'enable CCP1,  TMR1 overflow interrupt this if from some code I did for 18F452 and there is error in it, but the sample code is not complete, but should get you going. Smiley
You need to jump over the Int
 I could look for the finished code but would need time to find it.
why the NOP DelayMS would be better

Code:
    '16 BIT CCP1 PWM 
    Input PORTC.2        ' Disable the HPWM output while things are being setup
    RCON.7 = 0   ' 0 DISABLES PRIORITY LEVELS ON INTERRUPTS (16F COMPATABILITY)
    T1CON = %10110000   ' Turn off Timer1 with a Prescaler value of 1:8  
    PIE1 = %00000101    'enable CCP1,  TMR1 overflow interrupt  
    INTCON = %11000000  'enable global and peripheral interrupts
    Output PORTC.2      ' Set PORTC.2 (CCP1) to output
    

    T3CON = %00000000
   
    CCPR1H = wDUTY.HighByte    
    CCPR1L = wDUTY.LowByte    
    
    
    On_Hardware_Interrupt GoTo HARDWARE_INTERRUPT    
                                      
    GoTo START    'Jump over the subroutines
                                                                      
    
    '********** HARDWARE TMR 1 Interrupt **********
    HARDWARE_INTERRUPT:  
    Context Save    
         
'

    PIR1.2 = 0 ' CLEAR THE CCP1 INT FLAG          
    High PORTC.2          ' 
    'clear interrupt flag and exit...    
    bcf PIR1,0  
    Context Restore  
    retfie fast    
    '********** END HARDWARE TMR 1 Interrupt **********
 
      
    '***********************************************
  
    START:
  
« Last Edit: December 18, 2009, 12:18:54 12:18 by pickit2 » Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
king
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 2
-Receive: 21


Jack of All But Master of One


« Reply #2 on: December 18, 2009, 02:51:17 14:51 »

Thanks for the answer pickit2 but problem still continues after settign PIE1 = %00000111 and main body of the program is not executing only Interrupt service routine is executing and when i make pie1.1=0 then main body starts to execute normaly, if you have any other idea or find any wrong thing in my code then please guide me about this.IF anybody else try to find bug in my code then i will be very thankful to him.

Regards

King
Logged
king
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 2
-Receive: 21


Jack of All But Master of One


« Reply #3 on: December 19, 2009, 11:19:56 11:19 »

Hi!
 
I got success in producing Software PWM of 20KHz using Timer1 and now my code is executing without any error.

Regards

King
Logged
oldvan
Senior Member
****
Offline Offline

Posts: 372

Thank You
-Given: 154
-Receive: 107


If the van is a Rockin'...


WWW
« Reply #4 on: December 19, 2009, 03:42:15 15:42 »

King:  It would be good if you posted your working code here so others could benefit from it.








Edited to correct a couple typos.   Shocked
« Last Edit: December 21, 2009, 07:58:00 07:58 by oldvan » Logged

Give a man a fish and you feed him for a day.
Teach a man to fish and he will sit around in a boat drinking beer all day.
king
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 2
-Receive: 21


Jack of All But Master of One


« Reply #5 on: December 21, 2009, 04:16:31 04:16 »

Here is the Code;

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 14/12/2009                                        *
'*  Version : 1.0                                               *
'*  Notes   : Revised Ver2.0 with Three PWM's of 20KHz frequency*
'*          : two PWM's are Hardware and one is Software using Timer1*
'****************************************************************
Device 16F876a
XTAL = 20                                                                 
ALL_DIGITAL = true
declare ccp1_pin PORTC.2
DECLARE CCP2_PIN PORTC.1
TRISA = %0000000

Symbol T1CKPS0 = T1CON.4    ' Timer1 Input Clock Prescale Select bits
Symbol T1CKPS1 = T1CON.5    ' Timer1 Input Clock Prescale Select bits

Dim InterruptCount         As Byte
Dim Count_10_Interrupts    As Byte
Dim Count_100_Interrupts   As Byte
Dim Count_1000_Interrupts  As Word
Dim TimerOffset            As Word
dim a as word


'the offset & prescaler setting you selected in the Pic Timer calc tool is below

TimerOffset = 65435'65460'65510'65485'65410'60535
timeroffset=timeroffset+30      ' To compensate 0.6V variation in duty cycle                                                         
T1CKPS0 = 0                                                                   
T1CKPS1 = 0                                                                                         


Dim TimerSetLow As TimerOffset.LowByte
Dim TimerSetHigh As TimerOffset.HighByte

PIE1.0 =   1                    'enable TMR1 interupts

ccp1con.2=1
ccp1con.3=1
ccp2con.2=1
ccp2con.3=1

pie1.1=1                        'enable timer2 to pr2 march interrupt

INTCON.6 = 1                    'enable all unmasked interrupts
INTCON.7 = 1                    'enable Global interrupts
PIR1.0 =   0                    'reset interupt flag
T1CON.0 = 0                     'stop the timer
TMR1H = TimerSetHigh            'load a start up value into the timer
TMR1L = TimerSetLow
T1CON.0 =  0                    'start the timer
On_Interrupt goto ISR           'Interrupt Service Routine
hpwm 1,150,20000
hpwm 2,204,20000
low porta.3                                       
goto main
'**********************START ISR( INTERRUPT SERVICE ROUTINE )*****************
ISR:
if pir1.1=1 then
T1CON.0 = 0                     'stop the timer
TMR1H = TimerSetHigh            'load a start up value into the timer
TMR1L = TimerSetLow
PIR1.0 = 0                      'reset interupt flag
pir1.1=0
high portc.4
t1con.0=1
endif

if pir1.0=1 then
T1CON.0 = 0                     'stop the timer
low portc.4
pir1.0=0
endif

context restore

'************************END ISR( INTERRUPT SERVICE ROUTINE)*****************
Main:
toggle porta.3
'toggle 7
delayms 250

goto main
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC