Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Microcontroller Output Problem

Status
Not open for further replies.

DL_4000

New Member
Hi everyone,

I seem to have come across a small problem in my current project.

I am using a PIC16F648A microcontroller that is programmed to take digital inputs, with the aid of buttons, across two pins of PORTA. According to which button is pressed and ultimately which pin registers a high edge, the microcontroller is supposed to either give out a constant 5V high or a PWM of 4.5V for a few seconds out of the same PORTB.0 pin. The program loop constantly checks for input activity.

Now here is the problem. The moment I press the button that initiates the PWM command, the microcontroller stops responding to the button that initiates the constant 5V high afterwards but keeps responding to the button that initiates the PWM. I have tried another two 16F628A microcontrollers but they seem to have the same problem.

Here is my code, I am using PICBasic Pro:

trisa = %00000011 'PORTA.0 and PORTA.1 are defined as inputs
trisb = %00000000 'All PORTB ports are defined as outputs
CMCON = %00000111 'Comparators for PORTA.0-3 are switched off

Main:

IF porta.0 = 1 THEN
portb.0 = 1
PAUSE 3000 'Pausing for 3 seconds
portb.0 = 0
ENDIF

PAUSE 5

IF porta.1 = 1 THEN
PWM portb.0, 230, 600
ENDIF

PAUSE 5

GOTO Main

END

Regarding the PWM command in the code; there are 230 steps(out of 256), which determine the duty cycle(0 = 0% and 255 = 100% duty cycle) and ultimately the average voltage. There are also 600 cycles of 5ms that add up to 3 seconds in duration.

I have also tried different IF-THEN setups that jump to a different label to initiate the action and jump back to the Main loop but they display the same problem. I am using an external 4MHz crystal with pump charge capacitors.

Any help is appreciated.

Thanks
 
Do you have pull-down resistors on the switch inputs?

Hey there,

Yes I have both switches in pull-down mode, so there is no voltage rise unless I press any of the switches.

It is weird. If i code the microcontroller to output a constant 5V rise(no PWM) on the same output pin if any of the switches is pressed then there is no problem and the controller reacts to both inputs afterwards. Same goes for PWM on the same output pin if any of the switches is pressed. The moment I assign constant 5V output to one switch and PWM to the other switch this problem occurs.

Cheers
 
How are you distinguishing a PWM output (of average 4.5V) from a constant 5V? Are you using an oscilloscope? I would say you first try the two outputs in two different pins, and see if it works.
 
How are you distinguishing a PWM output (of average 4.5V) from a constant 5V? Are you using an oscilloscope? I would say you first try the two outputs in two different pins, and see if it works.

I am using an oscilloscope to check that I am getting true PWM. The microcontroller responds to both switches at any point in time if I use two different output pins. My problem is the use of one output pin for both inputs. I am trying to figure out if it is a physical controller barrier that cannot be overcome or a setting that needs to be configured through coding.

Thanks for the help, I appreciate it.
 
Last edited:
Have you tried writing the entire PORTB value instead of just setting bit 0? I wonder if there's a read/modify/write issue. Can you post the assembly file your compiler generates?
 
I was looking at the PICBASIC manual for PWM Pin,Duty,Cycle, and I found this "Pin is made an output just prior to pulse generation and reverts to an input after generation stops". Does this mean after the PWM cycle is over, it sets the PIN as digital I/P?
 
I was looking at the PICBASIC manual for PWM Pin,Duty,Cycle, and I found this "Pin is made an output just prior to pulse generation and reverts to an input after generation stops". Does this mean after the PWM cycle is over, it sets the PIN as digital I/P?

I read that section as well. It seems to set the pin to input after PWM is complete but is always ready to generate PWM output if prompted to do so i.e. a switch is pressed. As I said before, the pin outputs PWM whenever I press the button and the same goes for constant 5V as long as it is a separate output pin.

Thanks
 
Last edited:
Have you tried writing the entire PORTB value instead of just setting bit 0? I wonder if there's a read/modify/write issue. Can you post the assembly file your compiler generates?

By writing the entire PORTB value, do you mean output across all PORTB pins? I will post the assembly file tonight, I am not at my home computer at the moment.

Thanks
 
I would try redefining the PORTB pin to output using TRISB after the PWM cycle is over, and see what happens. This won't be a problem for PWM output as the manual said it makes the PIN output for that purpose.
 
I would try redefining the PORTB pin to output using TRISB after the PWM cycle is over, and see what happens. This won't be a problem for PWM output as the manual said it makes the PIN output for that purpose.

I have tried what you have suggested and defined the trisb register at the start of the Main loop and it works fine until I press the switch that initiates the PWM. After the PWM is complete the portb.0 pin outputs a permanent 5V and does not drop to zero. Then I added 'portb.0 = 0' as the next line to sink the output every time the loop initiates and now it works perfectly fine. The controller responds to both inputs at any given time using one output pin.

I feel as if this is kind of going around the problem rather than fixing it and am still trying to find out what is causing it.

Here is the assembly file of the original code as displayed in my first post.

Thank you guys so much for your help and sorry for the late reply, I live in Australia and the time zone is quite a bit out of sync.

Cheers
 

Attachments

  • TEST3..ASM
    1.8 KB · Views: 143
What I was interested in was the assembly code generated for the PWM command, but it looks like your compiler doesn't generate a full ASM file...that one still has the macros in it. The PWM code should be in one of the included files: TEST3.MAC or PBPPIC14.LIB. But the second one is probably proprietary, so you can't post it.
 
What I was interested in was the assembly code generated for the PWM command, but it looks like your compiler doesn't generate a full ASM file...that one still has the macros in it. The PWM code should be in one of the included files: TEST3.MAC or PBPPIC14.LIB. But the second one is probably proprietary, so you can't post it.

I am not very familiar with assembly language but you are right about the proprietorship of the pbppic14.lib file.

I have included the .MAC file, had to zip it due to its format not being supported by the upload manager.

Cheers
 

Attachments

  • TEST3..zip
    526 bytes · Views: 105
Last edited:
I gave the wrong code in the file. This is the right one. The previous MAC file included modifications that made the controller work properly. Don't think it really matters since you just want the PWM part but to be on the safe side.
 

Attachments

  • TEST3..zip
    526 bytes · Views: 95
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top