|
|
View previous topic :: View next topic |
Author |
Message |
greeniemax
Joined: 05 Jun 2013 Posts: 15
|
|
Posted: Sat Dec 14, 2013 5:18 am |
|
|
Thanks for the tip, I'll try to put it in somewhere around 100 to have little damping.
Okay sorry about the code, I couldn't understand what was needed let me post it here.
Main Program
Code: | #include <M1828C.h>
#include <2416.C>
#int_EXT
void EXT_isr(void)
{
if (Detected)
{
Loc++;
if (Loc >= Picks)
Loc = 0;
Pos = read_ext_eeprom(Loc);
printf("G%d\r",Pos);
}
Sec1 = 0;
}
#int_TIMER0
void TIMER0_isr(void)
{
TimerX++;
if (TimerX >= 61)
{
Sec++;
if (Sec >= 250)
Sec = 0;
Sec1++;
if (Sec1 >= 250)
Sec1 = 250;
}
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit); //16.3 ms overflow
setup_timer_4(T4_DISABLED,0,1);
setup_timer_6(T6_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
init_ext_eeprom();
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_16MHZ|OSC_INTRC|OSC_PLL_OFF,0);
//Start
setup_motor();
Picks = read_ext_eeprom(2);
int1 After_Power = true;
while(TRUE)
{
if (Sec != BSec)
{
Detected = ext_eeprom_ready();
BSec = Sec;
}
if (Detected)
{
if (EEPROM_out)
{
Pos = -Pos;
printf("G%d\r",Pos);
delay_ms(200);
EEPROM_out = false;
Loc = 0;
}
if (After_Power)
{
int LV1, LV2;
LV1 = read_eeprom(1);
LV2 = read_eeprom(2);
Loc = (LV1 + LV2 * 256)+1;
After_Power = false;
}
output_high(EEPROM_LED);
}
if (!Detected)
{
output_low(EEPROM_LED);
EEPROM_out = true;
}
if (Sec1 >= 2)
{
//Power Fail Detected
unsigned int LV1 = Loc & 0xFF;
unsigned int LV2 = Loc >> 8 & 0xFF;
write_eeprom(1,LV1);
write_eeprom(2,LV2);
}
}
}
void setup_motor(void)
{
output_low(EEPROM_LED);
delay_ms(3000);
fprintf(PORT4,"D0\r");
delay_ms(100);
fprintf(PORT4,"M255\r");
} |
Header File
Code: | #use FIXED_IO( C_outputs=PIN_C7,PIN_C6 )
#define CENTER PIN_A0
#define PFD PIN_A1
#define IRCUTTER PIN_A2
#define CENTER_LED PIN_C6
#define EEPROM_LED PIN_C7
#define EEPROM_SDA PIN_B4
#define EEPROM_SCL PIN_B6
//#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,bits=8,stream=PORT1)
//!#use rs232(baud=9600,parity=N,xmit=PIN_C0,rcv=PIN_C1,bits=8,stream=PORT2)
//!#use rs232(baud=9600,parity=N,xmit=PIN_C2,rcv=PIN_C3,bits=8,stream=PORT3)
#use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8,stream=PORT4)
#use i2c(Master,Fast,sda=PIN_B4,scl=PIN_B6)
unsigned int TimerX = 0;
unsigned int Sec = 0;
unsigned int BSec = 0;
int1 Detected = false;
int1 EEPROM_out = false;
unsigned int16 Loc = 0;
int Pos = 0;
int Picks = 0;
unsigned int Sec1 = 0;
void setup_motor(void); |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Sat Dec 14, 2013 6:31 am |
|
|
comment: I downloaded the PDF of the first servo motor. Nice unit, should be easy to use.
However..I'd get rid of 99% of your code. NO interrupts, timers, etc. That motor is TTL UART compatible, meaning you can connect the PIC to it's rcv wire and send simple ASCII to it, just follow the example they give in the datasheet. In fact I'd start with Hyperterminal and try to do exactly what they show first (might need MAX232 though). It's way to hard to debug code AND hardware at the same time. Confirm the commands do work, motor spins, etc.
THEN cut PIC code to say spin motor, confirm, test another command, confirm and build upon your work.
Once you've got the motor 'up and running' by simple PIC program THEN you can get 'fancy' with timers, eeprom, interrupts, etc.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
|
|
Posted: Sat Dec 14, 2013 10:06 am |
|
|
Ditto on get rid of the interrupts. Especially since you have the same UART used both inside and outside the ISR. Hopefully the compiler _will_ be generating a warning that it is disabling interrupts because of this.
Then the key one (said setup would be the problem...). You are specifying Port C to use 'fixed_IO', which means _you_ have to specify which pins are outputs. Then trying to send serial data out on PIN_C4, which is now set as an input...
You need to add C4 to the output pins specified. |
|
|
greeniemax
Joined: 05 Jun 2013 Posts: 15
|
|
Posted: Sat Dec 14, 2013 10:29 pm |
|
|
I'll try to add PINs to the list that are suppose to be TX but C4 works fine, B7, C0, C2, don't work.
Let me try that, thanks for the tip. |
|
|
greeniemax
Joined: 05 Jun 2013 Posts: 15
|
|
Posted: Sat Dec 14, 2013 11:03 pm |
|
|
I added fixed IO outputs for C0, C2, B7, didn't work.
I commented out the line of #use FIXED_IO( that didn't work either.
Thought I don't really need interrupt that often, I did add a simple Move = true and moved the process in the main program and that got ride of the warnings.
I really can't understand why I can't even have two serial ports when PIC16F1828 supports 2 hardware ports?
It's really confusing me, worst of all, I could use 2 ports easily on PIC16F684, which doesn't have any UART.
Is there any way to force Software ports?
Though I did make a change in the program, when Proteus was giving me warning about using TRIS and I found in forum here that I should go to Device Editor->Other Features->TRIS = False.
Which I did, might this be causing problem?
Let me enable it again and recompile and check. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Sun Dec 15, 2013 6:57 am |
|
|
comment:You're making your project too complicated and have misread the PIC info.
comment: Please re-read the datasheet, the 1828 does NOT have 2 hardware UARTS.It does however allow you to _choose_ which pair of pins are connected to the hardware UART peripheral.You decide whether to use RC4,RC5 OR RB5,RB7 as the UART.Think of it like a form C relay,you can have power go to EITHER a NC or a NO contact but NOT both.
comment: I'm unaware of any PIC having 4 _hardware_ UARTs though many now have 2.For your project you could stay with the 1828 and use 4 _software_ UARTs that CCS will easily create code for.
comment: I downloaded the 'smatrt servo motor' datasheet and it should be very easy to cut code to control them using the 'serial' mode.First I would code to test one motor,confirm,then add another,confirm,etc. I would NOT use the hardware UART to control a motor,save it for PC communications either for testing purposes and/or final MMI control.
comment:You do NOT need timers,fixed_io(),interrupts,etc. at this stage.Just cut simple 'test' code to get a servo running,build upon what you learn.
comment:IF/when you decide you need data FROM the servos,you'll need 4 rcv pins in addition to the 4 tx pins, so you need to be careful about choosing them!
wish I had one of those 'smart'servos.be fun to play with!
please, start over again, show us your code, we'll be happy to help( beats shovelling snow !!)
hth
jay |
|
|
greeniemax
Joined: 05 Jun 2013 Posts: 15
|
|
Posted: Sun Dec 15, 2013 7:39 am |
|
|
Just one question, how do I tell compiler to use software uart and not hardware? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Sun Dec 15, 2013 8:38 am |
|
|
easy: add 'FORCE_SW' to the USE RS232(...options...)
comment: if you press F11 while your project is open, you'll have direct access to the 'help' files that CCS provides. i keep it open whenever I'm programming.It's fast and easy to find answers to 99% of what you need to know!
hth
jay |
|
|
greeniemax
Joined: 05 Jun 2013 Posts: 15
|
|
Posted: Sun Dec 15, 2013 9:28 am |
|
|
Thanks a lot, FORCE_SW solved the problem.
I've already tested the motor with other PIC but because I didn't had enough pins on 14-Pin PIC16F684, I used PIC16F1828, which is newer and had 20 pins and 18 I/O.
Motor actually works great, accurate like crazy.
Thanks a lot for your support. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Sun Dec 15, 2013 10:24 am |
|
|
nice to hear you got it 'up and running'.
comment: Yes, PICs never seem to have enough pins !! I've finally settled on the 18F46k22 for 99% of my projects and products.Tons of RAM,2 UARTS,timers,etc.Allows me to use a common PCB for everything.
maybe a buck more but 'universal'.
cheers
jay |
|
|
|
|
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
|