/* * main.c */ void main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P1DIR |= 0xff; P2DIR |= 0xff; lcdinit(); LCD_write_string(5,0,"Hello!"); while(1); }
int main() { DDRB=0xff; DDRD=0x07; init_LCD(); // initialization of LCD _delay_ms(50); // delay of 50 mili seconds LCD_write_string("A"); // function to print string on LCD return 0; }
/******************************* 主程序 *******************************/ void main(void) { unsigned char i; float lux_data; //光数据 delay_nms(10); //lcd上电延时 LCD_init(); //lcd初始化 i=I2C_Write(0x01); //BH1750 初始化 delay_nms(10); while(1){ //循环 i=I2C_Write(0x01); //power on i=I2C_Write(0x10); //H- resolution mode TWCR=0; //释放引脚 delay_nms(180); //大约180ms if(i==0){ lux_data=I2C_Read(); //从iic总线读取数值 lux_data=(float)lux_data/1.2; //pdf文档第7页 conversion(lux_data); //数据转换出个,十,百,千 位 LCD_write_string(7,0,display);//显示数值,从第9列开始 } } }
int main() { DDRD=0xff; DDRB=0x07; _delay_ms(50); // delay of 50 mili seconds ADC_init(); // initialization of ADC /*variable init*/ float value; int temp; char str[5]; while(1) { init_LCD(); // initialization of LCD value=ADC_read(0); // function to read the Adc value temp=(value/1023)*5*100; // temp=100*voltage sprintf(str, "%d", temp); // function to convert int to string LCD_write_string(str); // function to print string on LCD _delay_ms(1000); // delay for visual } return 0; }
void main(void) { #if 0 // Config data in config.hex, implemented by app_config.c code Set_Pin_Drive_Mode(REG_PRT2_PC0, PRT_PC__DRIVE_MODE__STRONG); Set_Pin_Drive_Mode(REG_PRT2_PC1, PRT_PC__DRIVE_MODE__STRONG); Set_Pin_Drive_Mode(REG_PRT2_PC2, PRT_PC__DRIVE_MODE__STRONG); Set_Pin_Drive_Mode(REG_PRT2_PC3, PRT_PC__DRIVE_MODE__STRONG); Set_Pin_Drive_Mode(REG_PRT2_PC4, PRT_PC__DRIVE_MODE__STRONG); Set_Pin_Drive_Mode(REG_PRT2_PC5, PRT_PC__DRIVE_MODE__STRONG); Set_Pin_Drive_Mode(REG_PRT2_PC6, PRT_PC__DRIVE_MODE__STRONG); #endif LCD_init_4bit(2, LCD_FONT_5x10); LCD_moveto(0,0); LCD_write_string("Hello PSoC 2c"); LCD_moveto(0,1); LCD_write_data('X'); LCD_moveto(3,1); LCD_write_data('Y'); LCD_moveto(11,1); LCD_printf("ver %d", 1); #if 0 // NOTE: The following pin() functions have been moved to config.hex // LED: OUT: Port 6.0 Strong, initially OFF // DM 2,1,0: Strong: 1 1 0 (DATA = 1 ON, 0 OFF) Set_Pin_Drive_Mode(REG_PRT6_PC0, PRT_PC__DRIVE_MODE__STRONG); Clear_Pin(REG_PRT6_PC0); // LED: OUT: Port 6.6 Strong, initially OFF // DM 2,1,0: Strong: 1 1 0 (DATA = 1 ON, 0 OFF) Set_Pin_Drive_Mode(REG_PRT6_PC6, PRT_PC__DRIVE_MODE__STRONG); Clear_Pin(REG_PRT6_PC6); // Button: IN: Port 6.1 Pull Up // DM 2,1,0: Pullup: 0 1 0 (Data = 1 pullup 5K Vcc) [not 0 - hard 0V] Set_Pin_Drive_Mode(REG_PRT6_PC1, PRT_PC__DRIVE_MODE__RES_PULLUP); Set_Pin(REG_PRT6_PC1); #endif bool led2 = false; uint32_t count = 0; // Note: This is a busy wait loop - ok for a demo but not for product code. while(1) { if (Read_Pin_Raw(REG_PRT6_PC1)) Clear_Pin(REG_PRT6_PC0); else Set_Pin(REG_PRT6_PC0); delay_ms(10); if (count++ % 100 == 0) // 10 * 100 = 1000 ms led2 = !led2; if (led2) Clear_Pin(REG_PRT6_PC6); else Set_Pin(REG_PRT6_PC6); }; // don't return }
void LCD_write_int(unsigned char X,unsigned char Y,unsigned int num){ unsigned char *string="0X0000"; int_to_char(num,string); LCD_write_string(X,Y,string); }
void change_dir() { // Can eat? if (food.x == snake[snake_len-1].x && food.y == snake[snake_len-1].y) { snake[snake_len].x = food.x; snake[snake_len].y = food.y; if (++snake_len == MAX_LEN) snake_len = 0; screen[food.x][food.y] = SNAKE; score += 10; put_food(); } switch(direction) { case UP: { // Check for screen edge if (snake[snake_len-1].x == 0) { snake[snake_len].x = 5; snake[snake_len].y = snake[snake_len-1].y; } else { snake[snake_len].x = snake[snake_len-1].x-1; snake[snake_len].y = snake[snake_len-1].y; } break; } case RIGHT: { // Check for screen edge if (snake[snake_len-1].y == WIDTH-1) { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = 0; } else { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = snake[snake_len-1].y+1; } break; } case DOWN: { // Check for screen edge if (snake[snake_len-1].x == HEIGHT-1) { snake[snake_len].x = 0; snake[snake_len].y = snake[snake_len-1].y; } else { snake[snake_len].x = snake[snake_len-1].x+1; snake[snake_len].y = snake[snake_len-1].y; } break; } case LEFT: { // Check for screen edge if (snake[snake_len-1].y == 0) { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = WIDTH-1; } else { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = snake[snake_len-1].y-1; } break; } } // Wrong bite if (screen[snake[snake_len].x][snake[snake_len].y] == SNAKE) { // Score to string and screen char ss[] = "Points: "; char s[10]; dtostrf(score, 3, 0, s); strcat(ss,s); LCD_clear(); LCD_write_string(0,0," GAME OVER "); LCD_write_string(0,2,ss); // End game game = 0; return; } // Mark slice as inactive snake[snake_tail].x = -1; // Keep inside array limit if (++snake_tail == MAX_LEN) snake_tail = 0; if (++snake_len == MAX_LEN) snake_len = 0; update_snake(); }
int main(void) { double axg = 0, ayg = 0, azg = 0; double gxds = 0, gyds = 0, gzds = 0; //init mpu6050 mpu6050_init(); _delay_ms(50); LCD_init(); LCD_clear(); put_food(); // INtro while (1) { // Read current parameters mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds); LCD_write_string(0,0," Little Snake "); LCD_write_string(0,3," >> Move << "); if (axg > LIMIT || axg < -LIMIT ||ayg < -LIMIT || ayg > LIMIT) break; } // Init snake, both on screen and array screen[3][5] = SNAKE; screen[3][6] = SNAKE; screen[3][7] = SNAKE; snake[0].x = 3; snake[0].y = 5; snake[1].x = 3; snake[1].y = 6; snake[2].x = 3; snake[2].y = 7; snake_tail = 0; snake_len = 3; for (int z=snake_len; z<MAX_LEN; z++) snake[z].x = -1; while(game) { // Read current parameters mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds); // Take a decision if(axg > LIMIT) {if (direction != UP) direction = DOWN;} else if(axg < -LIMIT) {if (direction != DOWN) direction = UP;} if(ayg > LIMIT) {if (direction != LEFT) direction = RIGHT;} else if(ayg < -LIMIT) {if (direction != RIGHT) direction = LEFT;} // Make it happen change_dir(); // Check end game if (!game) break; LCD_init(); LCD_clear(); // Print entire screen for (int x=0; x<HEIGHT; x++) { for (int y=0; y<WIDTH; y++) { if (x == snake[snake_len-1].x && y == snake[snake_len-1].y) { LCD_write_char(HEAD); } else if (screen[x][y] == SNAKE) { LCD_write_char(SNAKE); } else if (screen[x][y] == FOOD) { LCD_write_char(FOOD); } else { LCD_write_char(SPACE); } } } _delay_ms(250); } }