Esempio n. 1
0
void putU8(unsigned char number)
{
  char value[3]={0,0,0};

  while((number - 100)>=0)
  {
    number -= 100;
    value[2]++;
  }
  value[2] += '0';

  while((number - 10)>=0)
  {
    number -= 10;
    value[1]++;
  }
  value[1] += '0';

  value[0] = number + '0';

  UART_send_byte(32);	// space
  UART_send_byte(value[2]);
  UART_send_byte(value[1]);
  UART_send_byte(value[0]);
}
Esempio n. 2
0
void putstr(char *str)
{
  char ch;

  while((ch=*str)!= '\0')
  {
    UART_send_byte(ch);
    str++;
  }
}
Esempio n. 3
0
/*
 * read device ID
 */
void si7005_read_ID(){
	U8 ID;
	i2c_status st = I2C_OK;
	if(state != RELAX){
		error_msg("measurements are in process");
		return;
	}
	if((st = i2c_7bit_send_onebyte(0x11, 0)) == I2C_OK){
		if((st = i2c_7bit_receive_onebyte(&ID,0)) == I2C_OK){
			uart_write("got ID: ");
			printUHEX(ID);
			UART_send_byte('\n');
		}
	}
	if(st != I2C_OK){
		uart_write("can't read ID, errcode: ");
		printUHEX(st); UART_send_byte('\n');
	}
}
Esempio n. 4
0
/**
 * read motor number
 * @param N - readed Number
 * @return 0 in case of error
 */
U8 get_motor_number(U8 *N){
    int Ival;
    if(readInt(&Ival) && Ival > -1 && Ival < 3){
        *N = (U8) Ival;
        UART_send_byte('*'); // OK
        return 1;
    }else{
        error_msg("bad motor");
        return 0;
    }
}
Esempio n. 5
0
int main()
{
	UART_init(9600); // 把串口波特率配置为9600
	
	while(1)
	{
		delay();
		UART_send("www.rationMCU.com", 17);//串口发送字符串数组
		UART_send_byte('\n'); //串口发送换行符字节
	}
}
Esempio n. 6
0
void putS16(short number)
{
  char value[6]={0,0,0,0,0,0};

  if(number >= 0)
  {
    value[5]='+';
  }
  else
  {
    value[5]='-';
    number *= -1;
  }

  while((number - 10000)>=0)
  {
    number -= 10000;
    value[4]++;
  }
  value[4] += '0';

  while((number - 1000)>=0)
  {
    number -= 1000;
    value[3]++;
  }
  value[3] += '0';

  while((number - 100)>=0)
  {
    number -= 100;
    value[2]++;
  }
  value[2] += '0';

  while((number - 10)>=0)
  {
    number -= 10;
    value[1]++;
  }
  value[1] += '0';

  value[0] = number + '0';

  UART_send_byte(32);	// space
  UART_send_byte(value[5]);
  UART_send_byte(value[4]);
  UART_send_byte(value[3]);
  UART_send_byte(value[2]);
  UART_send_byte(value[1]);
  UART_send_byte(value[0]);
}
int main()
{
	uint16_t adc_value;
	
	UART_init(9600);
	ADC_Init(7);
	TIM32B0_MAT_Init(500);
	
	while(1)
	{
		adc_value = ADC_Read(7);
		UART_send_byte(adc_value>>8);
		UART_send_byte(adc_value);
	}
}
Esempio n. 8
0
/*
 * start pressure reading
 */
void si7005_read_P(){
	const U8 cmd[2] = {0x03, 0x01};
	i2c_status st = I2C_OK;
	if(state != RELAX){
		error_msg("measurements are in process");
		return;
	}
	st = i2c_7bit_send(cmd, 2, 1);
	if(st != I2C_OK){
		error_msg("can't send read sequence, err: ");
		printUHEX(st);UART_send_byte('\n');
		return;
	}
	state = WAITFORP;
}
int main()
{
	uint16_t adc_value;
	
	UART_init(9600);
	ADC_Init(7);
	
	while(1)
	{
		delay();
		adc_value = ADC_Read(7);
		UART_send_byte(adc_value>>8);
		UART_send_byte(adc_value);
	}
}
Esempio n. 10
0
/*
 * process state machine
 */
void si7005_process(){
	U8 T[2], b;
	i2c_status st;
	if(state == RELAX) return;
	if(state == WAITFORP || state == WAITFORT){ // poll RDY
		if((st = i2c_7bit_send_onebyte(0, 0)) == I2C_OK){
			if(i2c_7bit_receive_onebyte(&b,0) == I2C_OK){
				if(b) return; // !RDY
				if((st = i2c_7bit_send_onebyte(1, 0)) == I2C_OK)
					if((st = i2c_7bit_receive_twobyte(T,0)) == I2C_OK)
						display_data(T);
				state = RELAX;
				if(st){
					uart_write("can't read value, err: ");
					printUHEX(st); UART_send_byte('\n');
				}
			}
		}else{
			error_msg("can't poll !RDY, err: ");
			printUHEX(st); UART_send_byte('\n');
			state = RELAX;
		}
	}
}
Esempio n. 11
0
void I2C_EE_Upload(void)
{
	int percentage;
	uint8_t byte;
	unsigned int LEDbackup;
	int i;
	
	LEDbackup=LEDbyte;
	clear();
	write('S');
	write('e');
	write('n');
	write('d');
	write('i');
	write('n');
	write('g');
	
	for (i=CONFIGLENGTH;i<EEPROM_BYTES;i++)
	{
		if((i%100)==0){
		setCursor(0,1);
		percentage = (100*i)/(EEPROM_BYTES-CONFIGLENGTH);
			writenumber(percentage);
//		write('0'+(i/(EEPROM_BYTES-CONFIGLENGTH)*100)/100);
//		write('0'+((i/(EEPROM_BYTES-CONFIGLENGTH)*100)/10)%10);
//		write('0'+(i/(EEPROM_BYTES-CONFIGLENGTH)*100)%10);
		write('%');
		write(' ');
		write(' ');}
		LEDbyte = LEDGripple[(i/80)%10];
		setLEDS();
		I2C_EE_BufferRead(&byte, i, 1);
		UART_send_byte(byte);
	}
		clear();

	delay_ms(2000);
	clear();
	standby();

	LEDbyte=LEDbackup;
	setLEDS();
}
Esempio n. 12
0
/*
 * display readed value in tenths
 */
static void display_data(U8 *d){
	U16 udata = *((int *)d);
	long idata = 0L;
	switch (state){
		case WAITFORP: // display pressure
			udata >>=4; // get 12 bit data
			idata = (udata*10L)/16L - 240L;
			uart_write("P*10=");
		break;
		case WAITFORT: // display themperature
			udata >>=2; // get 14 bit data
			idata = (udata*100L)/32L - 5000L;
			uart_write("T*100=");
		break;
		default:
			return;
	}
	print_long(idata);
	UART_send_byte('\n');
}
Esempio n. 13
0
void putint(int number)
{
  char value[6]={0,0,0,0,0,0};

   while((number - 10000)>=0)
  {
    number -= 10000;
    value[5]++;
  }
  value[5] += '0';

  while((number - 1000)>=0)
  {
    number -= 1000;
    value[4]++;
  }
  value[4] += '0';

  while((number - 100)>=0)
  {
    number -= 100;
    value[3]++;
  }
  value[3] += '0';

  while((number - 10)>=0)
  {
    number -= 10;
    value[2]++;
  }
  value[2] += '0';

  value[1] = number + '0';
  value[0] = '\0';

  UART_send_byte(32);	// space
  UART_send_byte(value[5]);
  UART_send_byte(value[4]);
  UART_send_byte(value[3]);
  UART_send_byte(value[2]);
  UART_send_byte(value[1]);
}
Esempio n. 14
0
int main() {
	unsigned long T = 0L;
	U8 rb, waitfor = 0;
	U8 *outbuf = "Hello, world";
	U8 inbuf[13] = {0};
	U8 inoutbuf[] = {'G','o','o','d','b','y','e',0};
	CFG_GCR |= 1; // disable SWIM
	// Configure clocking
	CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz
// Timer 4 (8 bit) used as system tick timer
	// prescaler == 128 (2^7), Tfreq = 125kHz
	// period = 1ms, so ARR = 125
	TIM4_PSCR = 7;
	TIM4_ARR = 125;
	// interrupts: update
	TIM4_IER = TIM_IER_UIE;
	// auto-reload + interrupt on overflow + enable
	TIM4_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;

	// PC2 - PP output (on-board LED)
	PORT(LED_PORT, DDR)  |= LED_PIN;
	PORT(LED_PORT, CR1)  |= LED_PIN;

	uart_init();
	spi_init();

	// enable all interrupts
	enableInterrupts();

	// Loop
	do{
		if((Global_time - T > paused_val) || (T > Global_time)){
			T = Global_time;
			PORT(LED_PORT, ODR) ^= LED_PIN; // blink on-board LED
		}
		if(waitfor && spi_buf_sent()){
			if(waitfor == 1)
				uart_write(inbuf);
			else
				uart_write(inoutbuf);
			waitfor = 0;
		}
		if(UART_read_byte(&rb)){ // buffer isn't empty
			switch(rb){
				case 'h': // help
				case 'H':
					UART_send_byte(rb);
					uart_write("\nPROTO:\n+/-\tLED period\n"
					);
				break;
				case '+':
					UART_send_byte(rb);
					paused_val += 100;
					if(paused_val > 10000)
						paused_val = 500; // but not more than 10s
				break;
				case '-':
					UART_send_byte(rb);
					paused_val -= 100;
					if(paused_val < 500)  // but not less than 0.5s
						paused_val = 500;
				break;
				case 's':
					spi_init();
				break;
				case 'b':
					printUHEX(SPI_SR);
				break;
				case '1':
					spi_send_buffer(outbuf, 12, inbuf);
					waitfor = 1;
				break;
				case '2':
					spi_send_buffer(inoutbuf, 7, inoutbuf);
					waitfor = 2;
				break;
				default:
					UART_send_byte(spi_send_byte(rb)); // send received byte to SPI & write ans to UART
			}
		}
	}while(1);
}
Esempio n. 15
0
void error_msg(char *msg){
    uart_write("\nERROR: ");
    uart_write(msg);
    UART_send_byte('\n');
}
Esempio n. 16
0
void printUHEX(U8 val){
	uart_write("0x");
	UART_send_byte(U8toHEX(val>>4)); // MSB
	UART_send_byte(U8toHEX(val));    // LSB
}
Esempio n. 17
0
//Main function (execution starts here after startup file)
int main(void)
{
	int i;
	int e;
	uint16_t temperature;

	init_GPIO_pins();
	
	//Short delay during which we can communicate with MCU via debugger even if later user code causes error such as sleep state with no wakeup event that prevents debugger interface working
	//THIS MUST COME BEFORE ALL USER CODE TO ENSURE CHIPS CAN BE REPROGRAMMED EVEN IF THEY GET STUCK IN A SLEEP STATE LATER
	for (i = 0; i < 1000000; i++)
	{
		LED_on();
	}
	
	GPIO_Init_Mode(GPIOA,GPIO_Pin_0,GPIO_Mode_IN_FLOATING); //User button.
	GPIO_Init_Mode(GPIOC,GPIO_Pin_11,GPIO_Mode_IN_FLOATING); //Accelerometer interrupt.
	delay_init();
	LED_off();
	LCDINIT();
	home();
	clear();
	display(); //Surely some of these can be commented out.
	noCursor();
	noBlink();

	standby();

	UART_init();
	humidity_init();
	ADC_init();
	I2C_EEPROM();
	I2C_ACCEL_INIT();
	I2C_EE_LoadConfig();
	logging_timer_init();
	
//	I2C_EE_BufferWrite(Test_Buffer, EEPROM_WriteAddress1, 100);
//I2C_EE_BufferRead(buffer, 0, 100);

	/*while(1){
		if(LEDbyte==512){LEDbyte=1;}
		else {LEDbyte=LEDbyte<<1;}
		setLEDS();
		
	setCursor(0,1);
	temperature=getTemperature();
	writenumber( temperature/100);
	write('.');
	writenumber((temperature/10)%10);
		write(' ');

		write(0xDF);
	write('C');
	setCursor(0,0);
	writenumber(readhumidity(24)); //Needs real temperature
	write(' ');
	write('%');
	write('R');
	write('H');
		delay_ms(50);
		
		check_and_process_received_command();
		
	}*/
	//currentstate=UPLOADING;

	while(1)
	{
		switch (currentstate){
			
		case WAITING:
			if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) //Polling is probably ok here, since the loop will be very very fast.
			{
				currentstate=LOGGING;
				clear();
				write('S');
				write('t');
				write('a');
				write('r');
				write('t');
				write('i');
				write('n');
				write('g');
				delay_ms(2000);
				clear();
				I2C_EE_StartLog();
				TIM4->CNT=0;
			}
			break;

		case LOGGING:
			if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)){currentstate=WAITING;I2C_EE_FinishLog();break;} //Polling is probably ok here, since the loop will be very very fast.
			LEDbyte|= 1<<8;
			setLEDS();
			if (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) != RESET)
			{
				TIM_ClearFlag(TIM3, TIM_IT_Update);
				temperature = getTemperature();
				LogBuffer[0]=(temperature>>8)&0xFF;
				LogBuffer[1]=temperature&0xFF;
				LogBuffer[2]=readhumidity(LogBuffer[0]);
				I2C_ACCEL_READ();
				I2C_EE_Log(LogBuffer);
				setCursor(0,1);
				writenumber(temperature/100);
				write('.');
				writenumber(temperature%100);
				write(' ');
				write(0xDF);
				write('C');
				write(' ');
				setCursor(0,0);
				writenumber(25); //Needs real temperature
				write(' ');
				write('%');
				write('R');
				write('H');
				write(' ');
				LEDbyte&= ~(1<<8);
				setLEDS();
			}
			break;
		
		case UPLOADING:
			currentstate=WAITING;
			I2C_EE_Upload();
			break;
		
		case ERASING:
			currentstate=WAITING;
			I2C_EE_Erase();
			break;
		
		case STREAMING:
			if (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) != RESET)
			{
				TIM_ClearFlag(TIM3, TIM_IT_Update);
				temperature = getTemperature();
				LogBuffer[0]=(temperature>>8)&0xFF;
				LogBuffer[1]=temperature&0xFF;
				LogBuffer[2]=readhumidity(LogBuffer[0]);
				I2C_ACCEL_READ();
//				I2C_EE_Log(LogBuffer);
				for (e=0;i<ENTRYBYTES;i++)
				{
					UART_send_byte(LogBuffer[e]);
				}
				setCursor(0,1);
				writenumber(temperature/100);
				write('.');
				writenumber(temperature%100);
				write(' ');
				write(0xDF);
				write('C');
				write(' ');
				setCursor(0,0);
				writenumber(25); //Needs real temperature
				write(' ');
				write('%');
				write('R');
				write('H');
				write(' ');
				LEDbyte&= ~(1<<8);
				setLEDS();
			}
		break;
		}