Jump to content

Tar

Members
  • Content Count

    3
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Tar

  • Rank
    Newbrie
  1. Dave thanks a ton! I really appreciate the help.
  2. Dave thank you for your input. When I get home tonight I am going to remove the serial code from the interrupt and also try to cut down the number of instructions inside the interrupt and see if that helps. Other than that does my logic look correct or at least on the right path for what I am trying to accomplish? In the future I am going to have more code so I am trying to figure out pwm using interrupts. I think cycles will be "wasted" using delays, correct?
  3. I am trying to control a simple futaba servo by creating a pulse on pin 4 of portb. I am trying to create a pulse from 1ms to 2ms. I believe with the settings below tmr0 is interrupting ever 100us. I also generate an interrupt when the usart receives a character. If the character is '>' I increment the pw variable and if the character is '<' I decrement the pw variable. I am trying to use the timer to create the pulse so I don't have to use delays. Maybe I am going about this problem totally wrong so any suggestions are appreciated. I have been printing tmr0 to the serial port and I notice that very often it is less than 156. How is this happening? When the tmr0 interrupt happens I set it 156. Anyone see any problems with the code below? #include #include "main.h" //Define device config block asm { list p=PIC16F628 __config H'3FE9' ;XT,!CP,!WDT,!PWRT,MCLR,BODEN,LPV,!CPD } void printCharDec(char n); void sendChar(char n); void printCharDec(char n) { if(n >= 100) { sendChar(((n / 100) % 10) + '0'); } if(n >= 10) { sendChar(((n / 10) % 10) + '0'); } sendChar((n % 10) + '0'); sendChar(13); sendChar(10); } void sendChar(char n) { while(!test_bit(pir1, TXIF)) { nop(); } txreg = n; set_bit(txsta,TXEN); } unsigned char data; unsigned char pw; unsigned char count; ////////////////////////////////////////////// // Interrupt handler ////////////////////////////////////////////// void interrupt( void ) { if(test_bit(intcon,T0IF)) { if(count == 0) { set_bit(portb, 4); } if(count == pw) { clear_bit(portb, 4); } count++; if(count == 200) { count = 0; } tmr0 = 156; clear_bit(intcon,T0IF); } if(test_bit(pir1, RCIF)) { if(test_bit(rcsta, FERR) || test_bit(rcsta, OERR)) { clear_bit(rcsta,CREN); nop(); // Put a nop between bit operations on the same register to avoid PIC read-modify-write problems set_bit(rcsta,CREN); } data = rcreg; if(data == '<') { pw--; } else if(data == '>') { pw++; } if(pw < 10) { pw = 10; } if(pw > 20) { pw = 20; } printCharDec(tmr0); } } ////////////////////////////////////////////// // Main program entry ////////////////////////////////////////////// main() { set_bit(txsta,BRGH); spbrg = 25; OPTION_REG = 8; enable_interrupt(GIE); set_bit(intcon, T0IE); clear_bit(txsta,SYNC); // SYNC=0; // Async Mode set_bit(rcsta,SPEN); // SPEN=1; // Serial Port Enable set_bit(txsta,TXEN); // Transmit Enable set_bit(rcsta,CREN); // Constant Receive Enable set_bit(pie1,RCIE); // Receive Interrupt Enable set_bit(intcon,PEIE); // Peripherial Interrupt Enable //Init ports //A and B port initialization cmcon = 8; trisa = 0; trisb = 0x02; porta = 0; portb = 0; count = 0; pw = 10; tmr0 = 0; //Infinite loop while(1) { clear_wdt(); } }
×
×
  • Create New...