void main(void) { unsigned long sClk, pClk; char recieveString[100]; cli(); // SetSystemClock(CLK_SCLKSEL_RC32M_gc, CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc); GetSystemClocks(&sClk, &pClk); /* * Programmable interrupt controller configuration */ PMIC_CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; //enable all levels of interrupts PORTH_DIR = 0xFF; PORTQ_DIR = 0x0F; //port q lower 3 bits control access to usb and other stuff so get access with these two lines PORTQ_OUT = 0x07; //if using port F make this hex 5. /* * Serial set up */ //initialize the usart d0 for 57600 baud with 8 data bits, no parity, and 1 stop bit, interrupts on low (porth set to this for debugging purposes) USART_init(&serialStruct, 0xD0, pClk, (_USART_RXCIL_LO | _USART_TXCIL_LO), 576, -4, _USART_CHSZ_8BIT, _USART_PM_DISABLED, _USART_SM_1BIT); USART_buffer_init(&serialStruct, 100, 100); //initialize the circular buffers USART_enable(&serialStruct, USART_TXEN_bm | USART_RXEN_bm); //enable the USART serialStruct.fOutMode = _OUTPUT_CRLF; //append a carriage return and a line feed to every output. serialStruct.fInMode = _INPUT_CR | _INPUT_TTY | _INPUT_ECHO; //echo input back to the terminal and set up for keyboard input. sei(); while(1) { //USART_send(&serialStruct, "Hey, am I working?"); if(serialStruct.serStatus & _USART_RX_DONE) { USART_read(&serialStruct, recieveString); USART_send(&serialStruct, recieveString); } while (!(serialStruct.serStatus & _USART_TX_EMPTY)) { ; } } }
void VCOM_Serial2Usb(void) { static char serBuf [USB_CDC_BUFSIZE]; int numBytesRead, numAvailByte; //ser_AvailChar (&numAvailByte); numAvailByte = USART_available(&usart); if (numAvailByte > 0) { if (CDC_DepInEmpty) { for ( numBytesRead = 0; numBytesRead < numAvailByte && numBytesRead < USB_CDC_BUFSIZE; numBytesRead++) serBuf[numBytesRead] = USART_read(&usart); //ser_Read (&serBuf[0], &numAvailByte); CDC_DepInEmpty = 0; USB_WriteEP (CDC_DEP_IN, (unsigned char *)&serBuf[0], numBytesRead); } } }
/** * @brief Set up serial port and PWM, then set the PWM value * to the value sent over the serial port. * @param argc Argument count * @param argv[] Argument list * @return Error code */ int main(int argc, char const *argv[]) { unsigned long sClk, pClk; cli(); /*disable interrupts*/ /************ SET UP SYSTEM CLOCK *************/ SetSystemClock(CLK_SCLKSEL_RC32M_gc, CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc); GetSystemClocks(&sClk, &pClk); PMIC.CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; /************ SET UP TIMERS *************/ TCC0_CTRLA = TC_CLKSEL_DIV64_gc; /*Set clock divider*/ TCC0_CTRLB = TC_WGMODE_SS_gc | TC0_CCAEN_bm;/*Set waveform generation to single slope*/ TCC0_CTRLC = 0x00; /*Turn waveform generation output compare off*/ TCC0_CTRLD = 0x00; /*Turn off event action*/ TCC0_CTRLE = 0x00; /*Sets timer to 16bit mode*/ TCC0_INTCTRLA = 0x02; /*Sets the interrupt to overflow to med priority*/ TCC0_INTCTRLB = 0x02; /*Sets the Compare or Capture interrupt to med priority*/ TCC0_PER = PWM_LENGTH; /* Cycle length */ TCC0_CCA = PWM_MIN; /* Capture and Compare point during cycle */ /************ SET UP SERIAL PORT *************/ /*Initialize serial port to desired values*/ USART_init(&stU, 0xE1, /* Will use port E1 */ pClk, (_USART_TXCIL_MED | _USART_RXCIL_MED), FBAUD, BSCALE_FACTOR, _USART_CHSZ_8BIT, _USART_PM_DISABLED, _USART_SM_1BIT); /*Initialize a buffer for incoming and outgoing serial transmissions*/ USART_buffer_init(&stU, RX_BUFSIZE, TX_BUFSIZE); /*Set the input and output modes for the specified serial port.*/ stU.fInMode = _INPUT_CR | _INPUT_ECHO; stU.fOutMode = _OUTPUT_CRLF; /*Enable specified serial port*/ USART_enable(&stU, (USART_TXEN_bm | USART_RXEN_bm)); /************ SET UP OUTPUT PORT *************/ PORTC_DIR = 0xFF; /*Sets all the pins on PortC to be output pins*/ sei(); /*Enable interrupts*/ /************ PROGRAM LOOP *************/ int newCCA; char rxBuf[RX_BUFSIZE]; /*Send initial message, then wait for Tx to complete*/ USART_send(&stU, "Enter PWM Value Between 500-1000"); while (!(stU.serStatus & _USART_TX_EMPTY) ) { ; } while(1) { /*Wait until input termination arrives*/ if (stU.serStatus & _USART_RX_DONE) { /*Buffer string locally and clear Rx register*/ USART_read(&stU, rxBuf); /*If it's a valid command, process it*/ if (strlen(rxBuf) > 0) { /* Convert received value to an int. If it's within valid * range, set the PWM CCA, then send confirmation message */ newCCA = atoi(rxBuf); if ( (newCCA >= PWM_MIN) && (newCCA <= PWM_MAX) ) { TCC0_CCA = newCCA; USART_send(&stU, "Set new PWM"); while (!(stU.serStatus & _USART_TX_EMPTY) ) { ; } } } } } }
void main(void) { int receiveInt = 0; setInt = 0; setFlag = 0; //false unsigned long sClk, pClk; cli(); // SetSystemClock(CLK_SCLKSEL_RC32M_gc, CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc); GetSystemClocks(&sClk, &pClk); /* * Programmable interrupt controller configuration */ PMIC_CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; //enable all levels of interrupts PORTH_DIR = 0xFF; PORTQ_DIR = 0x0F; //port q lower 3 bits control access to usb and other stuff so get access with these two lines PORTQ_OUT = 0x07; //if using port F make this hex 5. /* * Serial set up */ //initialize the usart d0 for 57600 baud with 8 data bits, no parity, and 1 stop bit, interrupts on low (porth set to this for debugging purposes) USART_init(&serialStruct, 0xD0, pClk, (_USART_RXCIL_LO | _USART_TXCIL_LO), 576, -4, _USART_CHSZ_8BIT, _USART_PM_DISABLED, _USART_SM_1BIT); USART_buffer_init(&serialStruct, 100, 100); //initialize the circular buffers USART_enable(&serialStruct, USART_TXEN_bm | USART_RXEN_bm); //enable the USART serialStruct.fOutMode = _OUTPUT_CRLF; //append a carriage return and a line feed to every output. serialStruct.fInMode = _INPUT_CR | _INPUT_TTY | _INPUT_ECHO; //echo input back to the terminal and set up for keyboard input. /* * Timer E0 setup for servo PWM */ TCE0_CTRLA = TC_CLKSEL_DIV64_gc; //set timer to div/64 TCE0_CTRLB = 0x10 | TC_WGMODE_SS_gc; //turn on capture(CCAEN) and set waveform generation mode to PWM TCE0_CTRLC = 0x00; //turn off compares TCE0_CTRLD = 0x00; //turn off events TCE0_CTRLE = 0x00; //turn off byte mode TCE0_PER = 10000; //set the top of the period to 20ms TCE0_CCA = 350; //lower bound, datasheet says 600 microseconds(which should be 300) but that is to low so set it to this TCE0_INTCTRLA = 0x01; //turn on Overflow interrupt at low priority. /* * Port J configuration for pushbutton incrementing */ PORTJ_DIR = 0x00; //all pins as input PORTJ_INTCTRL = 0x05; //turn on both interrupts to low PORTJ_PIN0CTRL = 0x01; //set pin 0 so only rising edges trigger PORTJ_PIN1CTRL = 0x01; //set pin 1 so only rising edges trigger PORTJ_INT0MASK = 0x01; //mask interrupt 0 to only be fired by pin 0 PORTJ_INT1MASK = 0x02; //mask interrupt 1 to only be fired by pin 1 /* * PORT E configuration */ PORTE_DIR = 0xFF; PORTH_OUT = TCE0_CCA/10; sei(); while(1) { if(serialStruct.serStatus & _USART_RX_DONE) { USART_read(&serialStruct, receiveString); receiveInt = atoi(receiveString); if(receiveInt < 350 || receiveInt > 1150) USART_send(&serialStruct, "These are not the values you are looking for"); else TCE0_CCA = receiveInt; } if(setFlag) { setFlag = 0; //false itoa(setInt, sendString, 10); while(1) USART_send(&serialStruct, sendString); } } }