|
|
View previous topic :: View next topic |
Author |
Message |
koux
Joined: 03 Jul 2017 Posts: 2
|
Controlling PWM with Serial Port using PIC18F26K22 |
Posted: Mon Jul 03, 2017 2:14 am |
|
|
Hello guys i am trying to control a pwm motor with entering numbers on my keyboard from 0-10. In protheus simulation the code works fine. However when i upload the code to the hardware, it does not work as i want. I am using Termite 3.3 to transmit data and CCS C Compiler. Here is my code:
Code: |
#include <18f26k22.h>
#fuses NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#fuses INTRC_IO
#use delay(clock=4000000)
#use fast_io(a)
#use RS232 (baud =9600, bits =8, parity = N, xmit = PIN_C6, rcv = PIN_C7)
char klavye[80];
#int_rda
void serihaberlesme_kesmesi ()
{
disable_interrupts(int_rda);
gets(klavye);
printf("\n\rTEMP %s\n",klavye);
}
void main()
{
enable_interrupts(GLOBAL);
setup_timer_2(T2_DIV_BY_16,254,1);
setup_ccp1(CCP_PWM);
delay_us(20);
while(true)
{
enable_interrupts(int_rda);
set_pwm1_duty((int8)(klavye));
delay_ms(100);
}
}
|
I think the problem is sending the data from Termite. What do you think? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Jul 03, 2017 2:41 am |
|
|
Quote: | I think the problem is sending the data from Termite. What do you think? |
Problem is Proteus. See the sticky at the head of this forum.
Break the problem down into manageable pieces.
Start with a 1Hz LED to prove that processor is working.
Separately test RS232 link with ERRORS statement in #use RS232.
Test PWM with and without a motor.
Then put it all together.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Jul 03, 2017 3:37 am |
|
|
Your approach is fundamentally flawed.
The interrupt means _one_ character has been received. In the interrupt you can fetch _one_ character. You call gets, which will then hang the code inside the interrupt.
Realistically, if you are prepared to hang the code like this, there is no point in using the interrupt. Just call 'kbhit', and when a character arrives call gets.
To use the interrupt sensibly, you would need to write a 'buffered' version of gets, which then sets a flag to say a line is available, when it sees the end of line.
However the odds are the reason it is not working at all, is that there is something wrong with the hardware that Proteus is not telling you...
How are you connecting the RS232?.
When using the hardware UART, you _must_ have 'ERRORS' in the #use RS232.
Use the 'UART1' syntax, to make sure you are using the hardware UART.
Have you got the MCLR line pulled up?.
What smoothing is attached to the chip?.
The PWM is going to need values from 0 to 254. 0 to 10 is hardly going to give any control.
You are getting a string, not a numerical value. To convert this to an integer, is not done with the (int8) cast. You need to look at atoi. So it isn't going to work in Proteus if Proteus was actually working.....
Follow Mike's advice and get each part working 'step by step'.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Mon Jul 03, 2017 4:59 am |
|
|
Another problem...
This...
#use fast_io(a)
will cause you a LOT of problems....
When using fast_io()... you MUST also use set_tris()... !!
For 99.9999% of ALL PIC programs , fast_io is NOT required so let the compiler handle the DDRs of the I/O ports AUTOMATICALLY.
The solution is to delete the use fast_io()... line
This is just one more example of Proteus NOT functioning correctly.
With respect to the RS232 communications, perform a simple 'loopback' on the TTL side of the USB<>TTL adapter to confirm the 'PC side' of the hardware is 100% working. I am assuming a non-real-RS232-hardware PC of course, though some of us still use real PCs.
Jay |
|
|
koux
Joined: 03 Jul 2017 Posts: 2
|
|
Posted: Tue Jul 04, 2017 6:23 am |
|
|
Thank you guys i considered all of your suggestions and changed my algorithm and it worked!! |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Wed Jul 05, 2017 4:01 am |
|
|
Well, since you benefited from the forum wisdom, you should now post your working code for those who come after you with a similar problem! _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|