vikash23
Full Member level 2
Hi,
I am using pic24hj128gp502
I am generating a 1KHz signal using a function generator, 5Vpk-pk, 20% duty.
I need to measure the PWM usnig my PIC24 and see it in my UART2 port.
I have executed UART2 port and I can see a message that i send
I saw some guide regarding input capture compare but I didn't understand how to implement it in software
Guide: **broken link removed** ... 70198D.pdf
I have attached my code. Please have a look and let me know how to move further.
Also can I please know when the CPU reads the input of the PWM what it reads as ? as a binary or as a hex value?
I am using pic24hj128gp502
I am generating a 1KHz signal using a function generator, 5Vpk-pk, 20% duty.
I need to measure the PWM usnig my PIC24 and see it in my UART2 port.
I have executed UART2 port and I can see a message that i send
I saw some guide regarding input capture compare but I didn't understand how to implement it in software
Guide: **broken link removed** ... 70198D.pdf
I have attached my code. Please have a look and let me know how to move further.
Also can I please know when the CPU reads the input of the PWM what it reads as ? as a binary or as a hex value?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include "p24hxxxx.h" /* generic header for PIC24H family */ #include "ccp.h" #include "../../../Chapter 4 Code Examples/excerise 1 UART/CONU2.h" _FOSCSEL(FNOSC_FRC); // set oscillator mode for FRC ~ 8 Mhz _FOSC(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMD_NONE); // use OSCIO pin for RA3 _FWDT(FWDTEN_OFF); // turn off watchdog #define PWMin RPINR7bits.IC1R #define BUF_SIZE 32 #define iend 25 //delay loop is iend times jend instruction cycles #define jend 1000 char count[] = {'0','0','0','0', '0'}; char s[BUF_SIZE]; int PWM = 0; static char rxchar='0'; //received character unsigned char got_rx =0; //rx char flag //delay function void delay(void) { // 100 msec int i,j; for (i=0; i<iend; i++) for (j=0;j<jend; j++); } void __attribute__((interrupt, no_auto_psv)) _U2RXInterrupt(void) { rxchar = (char) (U2RXREG & 0x00ff); got_rx =1; // Clear the UART RX interrupt flag bit IFS1bits.U2RXIF = 0; } void __attribute__((interrupt, no_auto_psv)) _IC1Interrupt(void) { IFS0bits.IC1IF=0; } int main ( void ) { __builtin_write_OSCCONL(OSCCON & 0xbf) ; //clear bit 6 unlock PWMin = 6; //make pin 6 IC1 //programming input perpherial pin selects RPINR19bits.U2RXR = 10; // make pin RP10 U2RX I/O // programming output perpherial pin selects RPOR5bits.RP11R =5; //make RP11 U2TX __builtin_write_OSCCONL(OSCCON | 0x40) ; //set bit 6 lock T2CONbits.TON = 0; TMR2 = 0; PR2 = 25000-1; _T2IP = 1; // Interrupt priority T2CONbits.TON = 1; //0 0 0 0 00 0 0 0 0 1 iniPWMin(); initU2(); rxchar =0; got_rx= 0; // interrupt priorities _U2RXIP=4; _IPL =0; // } // Hyperterminal Startup Inro Screen clrscr(); //clear hyper terminal screen home(); putsU2("The MicroStick UART2 Experiment"); putU2(0x0a); //carriage return /line feed putU2(0x0d); putsU2("Type a character and watch the return"); putU2(0x0a); //carriage return /line feed putU2(0x0d); while (1) /* endless loop vary pot and capture reading*/ { // 2. delay for min time using timer1 read before next change PWM = readPWM(); //rxchar = getU2(); putU2(PWM); //Nop(); delay(); } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /* ** CONU2.h ** console I/O library for Explorer16 board */ // I/O definitions for the Explorer16 //#define CTS _RF12 // Cleart To Send, input, HW handshake //#define RTS _RF13 // Request To Send, output, HW handshake #define BACKSPACE 0x8 // ASCII backspace character code // init the serial port (UART2, 115200@32MHz, 8, N, 1, CTS/RTS ) void initU2( void); // send a character to the serial port int putU2( int c); // wait for a new character to arrive to the serial port char getU2( void); // send a null terminated string to the serial port void putsU2( char *s); // receive a null terminated string in a buffer of len char char * getsn( char *s, int n); // useful macros #define clrscr() putsU2( "\x1b[2J") #define home() putsU2( "\x1b[H") #define pcr() putU2( '\r')
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 /* ** CONIO.C ** Basic CONSOLE I/O for Explorer16 demonstration board */ #include "p24hxxxx.h" #include "conU2.h" #define FCY 7370000/2 //#define FCY 8000000/2 #define BAUDRATE 9600 #define BRGVAL ((FCY/BAUDRATE)/4)-1 #define U_ENABLE 0x8000 // enable the UART #define U_TX 0x0400 // enable transmission // init the serial port (UART2, 1200, 8, N, 1 ) void initU2( void) { U2BRG = BRGVAL; // BAUD Rate Setting for 9600 U2MODE = U_ENABLE; U2STA = U_TX; U2MODEbits.BRGH = 1; // high Speed mode } // initCon // send a character to the UART2 serial port int putU2( int c) { while ( U2STAbits.UTXBF); // wait while Tx buffer full U2TXREG = c; return c; } // putU2 // wait for a new character to arrive to the UART2 serial port -don't use during interrupts char getU2( void) { while ( !U2STAbits.URXDA); // wait for a new character to arrive // return U2RXREG; // read the character from the receive buffer }// getU2 // send a null terminated string to the UART2 serial port void putsU2( char *s) { while( *s) // loop until *s == '\0' the end of the string putU2( *s++); // send the character and point to the next one putU2( '\r'); // terminate with a cr / line feed } // putsU2 char *getsn( char *s, int len) { char *p = s; // copy the buffer pointer do{ *s = getU2(); // wait for a new character putU2( *s); // echo character if (( *s==BACKSPACE)&&( s>p)) { putU2( ' '); // overwrite the last character putU2( BACKSPACE); len++; s--; // back the pointer continue; } if ( *s=='\n') // line feed, ignore it continue; if ( *s=='\r') // end of line, end loop break; s++; // increment buffer pointer len--; } while ( len>1 ); // until buffer full *s = '\0'; // null terminate the string return p; // return buffer pointer } // getsn #ifndef __CCPDRV_H__ #define __CCPDRV_H__ void turnoff(void); void turnon(int setting); void setPWM(int setting); int iniPWMin (void); char readPWM( void); #endif
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include "p24hxxxx.h" /* generic header for PIC24H family */ void turnoff(void) { //turn off RB2, Timer2 and any OC1 configuration on this pin T2CON =0; OC1CON =0x0000; TRISBbits.TRISB2 = 1; } void turnon(int setting) { //turn on RB2, Timer2 and the PWM OC1 configuration on this pin PR2=0x07ff ; OC1R =setting; OC1RS =setting; OC1CON = 0x0006; //timer2 and edge aligned PWM T2CON =0x8000; } void setPWM(int setting) { turnoff(); turnon(setting); } void iniPWMin (void){ IC1CONbits.ICM = 0; IC1CON = 0x89; //00 0 00000 1 00 0 1 001 rising edge,buffer is empty,No input capture overflow,Interrupt on every second capture, TMR2 T2CON =0x8000; } char readPWM( void) { while ( !IC1CONbits.ICBNE); // wait for a new character to arrive return IC1BUF; // read the character from the receive buffer }