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.

Problem with PIC16f876 USART and hardware PWM in Assembly

Status
Not open for further replies.

Psyfusion

Newbie level 5
Joined
Mar 1, 2006
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,349
Hi!

I'm trying to develop a system with two PICs. One receives analog input and outputs serial data with usart to another PIC. That one outputs PWM with a duty cycle according to the received serial data.
My problem is that i can't get the pwm to work correctly when using the serial interface. I'm using the usart in async. mode. I can control the duty cycle by anything else(like parallel input on 8 pins) and i can also control it by the serial port but only randomly. But it stops when the code is waiting for a start byte to sync the reception. The first received byte modifies the duty cycle but then nothing can change it(the program is running although)! Without waiting for a start byte it changes constantly.

Please help me if you can!

Thanx!
 

Re: PIC USART and PWM

What chip are you using?
How do you create PWM output (hardware or software)?

Cheers,
 

Re: PIC USART and PWM

I'm using 16f876 and hardware pwm, the code is written in assembly. Every register is set right. I have been using hardware pwm for ages, but i couldn't get it working together with usart!
 

PIC USART and PWM

Hi I really need help! I have tryed several settings and i still couldn't get the duty cycle change according to the serial input!

Please help!
 

Re: PIC USART and PWM

Post your piece of code for setting PWM and USART modules, and the code for processing your USART and PWM.

Cheers,
 

Re: PIC USART and PWM

Hi Psyfusion
Your problem is not clear from your postings.
Is it one channel or several channels of PWM you wish to output?
Is it one at a time or all simultaneously?

I agree with Namqn
Post your piece of code for setting PWM and USART modules, and the code for processing your USART and PWM.

Polymath
 

Re: PIC USART and PWM

OK, here's the code...
(if something is really screwed up, then i might have copied the wrong code...sorry if so...)

R0L EQU 0x20
R0H EQU 0x21
R1L EQU 0x22
R1H EQU 0x23
R2L EQU 0x24
R2H EQU 0x25
R3L EQU 0x26
R3H EQU 0x27
R4L EQU 0x28
R4H EQU 0x29
R5L EQU 0x2A
R5H EQU 0x2B
ORG 0x0000
BCF PCLATH,3
BCF PCLATH,4
GOTO L0002
ORG 0x0004
RETFIE
L0002:
;pwm setup
BSF STATUS,RP0
BCF 0x07,2
movlw 249
movwf PR2
BCF STATUS,RP0
movlw b'00000101'
movwf T2CON
movlw 0x0C
movwf CCP1CON
;usart setup
BSF STATUS,RP0
MOVLW 0x81
MOVWF SPBRG
BSF TRISC,6
BSF TRISC,7
MOVLW 0x24
MOVWF TXSTA
BCF STATUS,RP0
MOVLW 0x90
MOVWF RCSTA
;main program
L0001:
;receive start byte for sync of data
CALL HS10
MOVWF 0x2C
;is start byte Ok?
MOVF 0x2C,W
SUBLW 0x41
BTFSS STATUS,Z
GOTO L0003
; if yes receive data
CALL HS10
MOVWF 0x2D
;received data to duty cycle reg
movf 0x2d,w
movwf CCPR1L
L0003: MOVLW 0x1F
ANDWF STATUS,F
GOTO L0001
L0004: GOTO L0004

;subrutines for serial reception

HS01: BTFSC PIR1,TXIF
GOTO HS02
GOTO HS01
HS02: MOVWF TXREG
RETURN
HS10: BTFSC PIR1,RCIF
GOTO HS11
GOTO HS10
HS11: MOVF RCREG,W
RETURN
END
 

Re: PIC USART and PWM

I couldn't see any wrong thing in your code, except
Code:
BSF TRISC,6
but it doesn't matter in your case.

I even tested your code in a PIC16F876A with 20 MHz, connecting to a PC at 9600 bps. The PIC can vary the pulse width according to the value sent from the PC (with 'A' as the start byte).

So, check your hardware and your transmitting PIC.

Cheers,
 

Re: PIC USART and PWM

namqn said:
I couldn't see any wrong thing in your code, except
Code:
BSF TRISC,6
but it doesn't matter in your case.


Whilst I agree that it does not matter in this case -- the 877A data sheet states:
Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be set in order to configure pins RC6/TX/CK and RC7/RX/DT as the Universal Synchronous Asynchronous Receiver Transmitter.

I think it is better for the pins to be inputs when USART releases control.

I would suggest BCF TRISC,2 replaces BCF 0x07,2
If you forget to set bank bits MPASM will give you a warning!
Better still
banksel TRISC
BCF TRISC, RC2


Polymath
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top