コード例 #1
0
ファイル: analog2serial.c プロジェクト: KPSdev/Analog2serial
int main(void){
USART_init();        //Call the USART initialization code
adc_init();
 
while(1){        //Infinite loop
 adc_value=read_adc(0);
 itoa(adc_value,buffer,10);
 USART_putstring(buffer);    //Pass the string to the USART_putstring function and sends it over the serial
 USART_send('\r');			//Carriage return
 USART_send('\n');			//Linefeed
 _delay_ms(500);
 }
 
return 0;
}
コード例 #2
0
ファイル: USART.c プロジェクト: bengeos/SDC-DAs
void USART_Put(char* StringPtr)
// sends the characters from the string one at a time to the USART
{
	while(*StringPtr != 0x00)
	{
		USART_send(*StringPtr);
		StringPtr++;
	}
}
コード例 #3
0
ファイル: keyring.c プロジェクト: PhincPhluphphyUnikorms/APC
void USART_putstring(char * streng){

	for (int i = 0; i < strlen(streng); i++){ 

		USART_send(streng[i]);    //send string
		
		PORTC ^= _BV(PC0); //Makes status LED blink

		_delay_ms(1);
	}

}
コード例 #4
0
ファイル: Usart.c プロジェクト: JeffHenry/Phys402
//-------------------------------------------------------------------------------------
// Function Purpose:	PartII: Send 5 messages out to putty via USART transmission.
//						Currently, only sends one message using the provided header 
//                      functions. 		
//-------------------------------------------------------------------------------------
int main(void)
{
	// Clock Variables:
	unsigned long sClk, pClk;
	cli();					// Clear Interrupts
	
	// Set up the serial clock
	SetSystemClock(CLK_SCLKSEL_RC2M_gc, CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc);
	GetSystemClocks(&sClk, &pClk);
	
	// Set up the timer:
	//TCC0_CTRLA = 0x04;						// Pre-scaler
	//TCC0_INTCTRLA = 0x02;						// Medium priority interrupts
	//TCC0_PER = 0xFFFF;						// Timer Period
	
	// BAUD Configurations 
	USART_init(&USARTinstance, OUTPUT_PORT, pClk,(_USART_TXCIL_LO | _USART_RXCIL_LO), 
				USART_BAUDRATE, NBSCALE_FACTOR, _USART_CHSZ_8BIT, _USART_PM_DISABLED, _USART_SM_1BIT);			
	
	// Buffer Configurations
	USART_buffer_init(&USARTinstance,160,80);						// Initialize buffer with 80 char for Tx and Rx
	USARTinstance.fInMode = _INPUT_ECHO | _INPUT_CR | _INPUT_TTY;	// Terminal Echo, Carriage Return Termination, and Keyboard Input
	USARTinstance.fOutMode = _OUTPUT_CR;							// Append Carriage Return to output
	
	USART_enable(&USARTinstance, USART_TXEN_bm | USART_RXEN_bm);		// Enable the Tx/Rx
	PMIC_CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;	// Enable low, medium, and high interrupts.

	sei();									// Enable interrupts
	
	// Port Configurations:
	PORTC_DIR |= 0x03;						
	PORTC_OUT &= 0xFE;						// Turn off PC0 to enable transceiver
	PORTC_OUT |= 0x02;						// Turn on PC1 to take transceiver out of shutdown mode
	
	// Messages to send:
	char* messages[5] = {"One\0", "Two\0", "Three\0", "Four\0", "Five\0"};
	PORTH_DIR = 0xFF;						// Used for visualizing function calls on LEDs
	
	// Test case || Attempted to send a string out to the putty console ||
	USART_send(&USARTinstance, "Testing Testing");
	while(USARTinstance.serStatus & _USART_TX_EMPTY	);		// Wait for the transmitter to be empty
	
	while(1)
	{
		// If the timer, toggled the semaphore, send a message.
		if(flag == true)
		{
			send_msg(messages[count]);
			count = (++count)%5;
		}
	}
}
コード例 #5
0
ファイル: SerialTest.c プロジェクト: nukesforbreakfast/IRbot
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)) { ; }
	}
}
コード例 #6
0
ファイル: part1.c プロジェクト: sylvesterink/phys402
/**
 * @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) ) { ; }
                }
            }
        }
	}
}
コード例 #7
0
ファイル: ServoTest.c プロジェクト: nukesforbreakfast/IRbot
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);
		 }
	}
}
コード例 #8
0
ファイル: main.c プロジェクト: kisieel/cFOF
int main()
{
	uint32_t data1, data2;
	
	SYS_TICK_init();
	
	data1 = SYS_TICK_timeOut(0,0);
	while (SYS_TICK_timeOut(1, data1) < 1000);
	
	// Keep power supply
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN;            // Clock for GPIOA
	GPIO_config(0x0A, 3, GPIO_MODE_GP, GPIO_PULL_Floating, GPIO_TYPE_Pushpull, GPIO_SPEED_400k, 0);	
	GPIO_config(0x0A, 7, GPIO_MODE_GP, GPIO_PULL_Floating, GPIO_TYPE_Pushpull, GPIO_SPEED_400k, 0);
	Power3VOn;
	Power5VOn;
	
	KEY_init();
	MENU_init();
	USART_init();
	
#ifdef USART_debug
	USART_send("Peripherals initialized.\n");
#endif
	
	RFM69W_init();
	_BUZZER_init();
	_LED_init();
	_LED_off();

#ifdef USART_debug
	USART_send("External devices initialized.\n");
#endif


	// Block device while the power button is still pressed
	while(GPIOA->IDR & GPIO_IDR_IDR_0);

	_BUZZER_alarm_set_vol_list(2);
	_BUZZER_alarm_set_tone_list(2);
	_BUZZER_alarm_set_tempo_list(2);

	for(;;) {
		_actual->menu_fun(GetKeys());
		
		// Holding key for GoUpTime will make menu to go up, to level 0
		if (GPIOA->IDR & GPIO_IDR_IDR_2) {
			data2 = SYS_TICK_timeOut(0, 0);
			while (GPIOA->IDR & GPIO_IDR_IDR_2) {
				// Keep blinking
				if (_actual == _Syg_1_menu || _actual->par == _Syg_1_menu)
					_LED_blink_on(MENU_LED_SYG1);
				if (_actual == _Syg_2_menu || _actual->par == _Syg_2_menu)
					_LED_blink_on(MENU_LED_SYG2);
				if (_actual == _Syg_3_menu || _actual->par == _Syg_3_menu)
					_LED_blink_on(MENU_LED_SYG3);
				if (_actual == _Syg_4_menu || _actual->par == _Syg_4_menu)
					_LED_blink_on(MENU_LED_SYG4);
				
				if (SYS_TICK_timeOut(1, data2) > GoUpTime) {
					_LED_set_color(MENU_LED_Menu, 0, 0, 0);
					_LED_on();
	#ifdef USART_debug
					USART_send("Going up to level 0.\n");
	#endif
					_actual = _actual->par;
			}
		}
	}
		
		// Holding key for TurnOffTime will make device to turn off
		if (GPIOA->IDR & GPIO_IDR_IDR_0) {
			data1 = SYS_TICK_timeOut(0, 0);
			while (GPIOA->IDR & GPIO_IDR_IDR_0) {
				// Keep blinking
				if (_actual == _Syg_1_menu || _actual->par == _Syg_1_menu)
					_LED_blink_on(MENU_LED_SYG1);
				if (_actual == _Syg_2_menu || _actual->par == _Syg_2_menu)
					_LED_blink_on(MENU_LED_SYG2);
				if (_actual == _Syg_3_menu || _actual->par == _Syg_3_menu)
					_LED_blink_on(MENU_LED_SYG3);
				if (_actual == _Syg_4_menu || _actual->par == _Syg_4_menu)
					_LED_blink_on(MENU_LED_SYG4);
				
				if (SYS_TICK_timeOut(1, data1) > TurnOffTime) {
					_LED_off();
#ifdef USART_debug
					USART_send("Power off detected. System backed up. Switching off.\n");
#endif
					Power3VOff;
					Power5VOff;
					// Preventing from further code execution
					for (;;);
				}
			}
		}
	}
}