void led_pov_next(unsigned char color) { static unsigned char autocolor; static unsigned char charcnt; // Character in the string static unsigned char colcnt; // Column in the character if(charcnt >= NUMCHARS) { charcnt = 0; povcycles++; autocolor++; if (autocolor > LED_SHOW_BLU) { autocolor = LED_SHOW_RED; } } led_showbin(color == LED_SHOW_AUTO ? autocolor : color, readcharmap(povstring[charcnt], colcnt)); delay_10us(40); led_showbin(color == LED_SHOW_AUTO ? autocolor : color, 0); colcnt++; if(colcnt >= NUMCOL) { colcnt = 0; charcnt++; delay_10us(80); } }
char sampleADC(void) { char res = 0x0; CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_VrefintCmd(ENABLE); delay_10us(3); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_6Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_0, ENABLE); delay_10us(3); ADC_SoftwareStartConv(ADC1); while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0); res = (char)ADC_GetConversionValue(ADC1); ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ //ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_0, DISABLE); return res; }
void LCD_init(void) { lcd_DB0_tris = 0; lcd_DB1_tris = 0; lcd_DB2_tris = 0; lcd_DB3_tris = 0; lcd_DB4_tris = 0; // Data0 lcd_DB5_tris = 0; // Data1 lcd_DB6_tris = 0; // Data2 lcd_DB7_tris = 0; // Data3 lcd_RW_tris = 0; lcd_RS_tris = 0; lcd_E_tris = 0; lcd_E = 0; lcd_RW = 0; delay_1ms(40); // > 40ms Init LCD_4write(0, 0, 0, 0, 1, 1); delay_1ms(5); LCD_4write(0, 0, 0, 0, 1, 1); delay_1ms(5); LCD_4write(0, 0, 0, 0, 1, 1); // Function set - 8 bits interface delay_1ms(5); LCD_4write(0, 0, 0, 0, 1, 0); delay_10us(25); /* DL is 4bits, N is 2 row, F is 0 */ LCD_8write(0, 0, 0, 0, 1, 0, 1, 0, 0, 0); delay_10us(25); /* Diplay on, Cursor, Blink */ LCD_8write(0, 0, 0, 0, 0, 0, 1, 1, 1, 1); delay_10us(25); /* Clear LCD */ LCD_8write(0, 0, 0, 0, 0, 0, 0, 0, 0, 1); delay_1ms(25); /* Set Entry Mode */ LCD_8write(0, 0, 0, 0, 0, 0, 0, 1, 1, 0); delay_1ms(25); while ((LCD_get_BF() & 0x80) == 1) ; delay_1ms(40); }
/** * @brief Read ADC1 * @caller several functions * @param None * @retval ADC value */ u16 ADC_Supply(void) { uint8_t i; uint16_t res; /* Enable ADC clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); /* de-initialize ADC */ ADC_DeInit(ADC1); /*ADC configuration ADC configured as follow: - Channel VREF - Mode = Single ConversionMode(ContinuousConvMode disabled) - Resolution = 12Bit - Prescaler = /1 - sampling time 9 */ ADC_VrefintCmd(ENABLE); delay_10us(3); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, ENABLE); delay_10us(3); /* initialize result */ res = 0; for(i=8; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* de-initialize ADC */ ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, DISABLE); return (res>>3); }
void DHT_Start(){ DHT11_SIGNAL_CONFIG = 0; // PORT RA1 as OUTPUT from MCU DHT11_SIGNAL = 1; delay_10us(5); DHT11_SIGNAL = 0; delay_1ms(30); DHT11_SIGNAL = 1; delay_10us(3); DHT11_SIGNAL = 0; delay_1us(1); DHT11_SIGNAL_CONFIG = 1; // PORT RA1 as INPUT to MCU }
void delay_ms(long t) // delays t millisecs { do { delay_10us(100); } while(--t); }
/** * @brief ADC_Icc(ADC_24 of ADC_0~27) initialization * @caller main and ADC_Icc_Test * @param None * @retval None */ void ADC_Icc_Init(void) { /* Enable ADC clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); /* de-initialize ADC */ ADC_DeInit(ADC1); /* ADC configured as follow: - NbrOfChannel = 1 - ADC_Channel_24 - Mode = Single ConversionMode(ContinuousConvMode disabled) - Resolution = 12Bit - Prescaler = /1 - sampling time 159 */ /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single,ADC_Resolution_12Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_24, ENABLE); /* disable SchmittTrigger for ADC_Channel_24, to save power */ ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE); /* a short time of delay is required after enable ADC */ delay_10us(3); }
void endMessage() { GIE=0; //wait until forwarding-Message is complete while (serialStatus & inTransmitMsgBit) { GIE=1; delay_10us(); delay_10us(); delay_10us(); delay_10us(); delay_10us(); GIE=0; } endMessageIntern(); GIE=1; }
int main(void) { #if STARTUP_DELAY /* Power ON delay (300ms) */ for (wait=0; wait<150; wait++) { delay_10us(200); // 2ms } #endif #if F_CPU == 12800000 /* RC OSC mode, (D-) */ TCCR0B = 3; /* 1/64 prescaler */ OSCCAL=118; #endif #if 1 /* PD5は USB D-, PD2はmodeジャンパ用 pullup */ /* なお、PD2はクロック出力を無効にした時に入力ポートになります */ PORTD |= ((1<<PD6)|(1<<PD5)|(1<<PD2)|(1<<PD1)|(1<<PD0)); /* all outputs except (USB data, PD2, PD1, PD0) */ DDRD = ~(USBMASK | (1<<PD6)| (1<<PD2)|(1<<PD1)|(1<<PD0)); #else DDRD = ~USBMASK; /* all outputs except USB data */ #endif PORTB = 0xff; /* All pullup */ DDRB = 0; /* PB7-0=in */ usbInit(); sei(); for(;;){ /* main event loop */ usbPoll(); } return 0; }
/** \brief This SWI function generates a Wake-up pulse and delays. * \return success */ uint8_t sha204p_wakeup(void) { swi_set_signal_pin(0); delay_10us(SHA204_WAKEUP_PULSE_WIDTH); swi_set_signal_pin(1); delay_ms(SHA204_WAKEUP_DELAY); return SHA204_SUCCESS; }
void writeEEPROMByte(const char address, char data) { M24LR04E_Init(); M24LR04E_WriteOneByte(M24LR16_EEPROM_ADDRESS_USER, address, data); M24LR04E_DeInit(); delay_10us(100); }
void pulse() { E = 0; __delay_us(5); E = 1; __delay_us(5); E = 0; delay_10us(10); }
void sendMessage(byte dest) { GIE=0; while (serialStatus & inSendQueueMsgBit) { //wait until we can use the buffer GIE=1; delay_10us(); delay_10us(); delay_10us(); delay_10us(); delay_10us(); GIE=0; } sendMessageIntern(dest); GIE=1; }
/** \brief This I2C function generates a Wake-up pulse and delays. * \return status of the operation */ uint8_t ecc108p_wakeup(void) { #if !defined(ECC108_GPIO_WAKEUP) && !defined(ECC108_I2C_BITBANG) // Generate wakeup pulse by writing a 0 on the I2C bus. uint8_t dummy_byte = 0; uint8_t i2c_status = i2c_send_start(); if (i2c_status != I2C_FUNCTION_RETCODE_SUCCESS) return ECC108_COMM_FAIL; // To send eight zero bits it takes 10E6 / I2C clock * 8 us. delay_10us(ECC108_WAKEUP_PULSE_WIDTH - (uint8_t) (1000000.0 / 10.0 / I2C_CLOCK * 8.0)); // We have to send at least one byte between an I2C Start and an I2C Stop. (void) i2c_send_bytes(1, &dummy_byte); i2c_status = i2c_send_stop(); if (i2c_status != I2C_FUNCTION_RETCODE_SUCCESS) return ECC108_COMM_FAIL; #else # if defined(ECC108_I2C_BITBANG) // Generate wakeup pulse using the GPIO pin that is connected to SDA. I2C_DATA_LOW(); delay_10us(ECC108_WAKEUP_PULSE_WIDTH); I2C_DATA_HIGH(); # else // Generate wakeup pulse by disabling the I2C peripheral and // pulling SDA low. The I2C peripheral gets automatically // re-enabled when calling i2c_send_start(). // PORTD is used on the Microbase. You might have to use another // port for a different target. TWCR = 0; // Disable I2C. //DDRD |= _BV(PD1); // Set SDA as output. //PORTD &= ~_BV(PD1); // Set SDA low. pinMode(SDA, OUTPUT); digitalWrite(SDA, LOW); delay_10us(ECC108_WAKEUP_PULSE_WIDTH); //PORTD |= _BV(PD1); // Set SDA high. digitalWrite(SDA, HIGH); # endif #endif delay_10us(ECC108_WAKEUP_DELAY); return ECC108_SUCCESS; }
//***************************************************************** void delay_ms(int ms) { unsigned char i; while(ms--) { i=4; while(i--) { delay_10us(25); } } }
// // Non interrupt driven beeper. Does not play well if interrupt-based sounds being played void sound_beep_polled() { unsigned char i; sound_amp_on(); delay_ms(100); for(i=0; i < 255; i++) { sound_val_polled( i & 0x01 ? 178 : 78); delay_10us(10); } sound_amp_off(); }
void LCD_write_byte(BYTE RS, BYTE ch) { /* Wait for any instruction being performed on LCD */ while ((LCD_get_BF() & 0x80) == 1) ; /* Wait done... continuing ... */ lcd_DB4_tris = 0; lcd_DB5_tris = 0; lcd_DB6_tris = 0; lcd_DB7_tris = 0; lcd_RS_tris = 0; lcd_RW_tris = 0; lcd_E_tris = 0; lcd_RW = 0; lcd_RS = RS; lcd_DB7_out = ((ch & 0x80) == 0x80); lcd_DB6_out = ((ch & 0x40) == 0x40); lcd_DB5_out = ((ch & 0x20) == 0x20); lcd_DB4_out = ((ch & 0x10) == 0x10); delay_1us(); delay_1us(); lcd_E = 1; delay_1us(); delay_1us(); lcd_E = 0; delay_1us(); delay_1us(); lcd_DB7_out = ((ch & 0x08) == 0x08); lcd_DB6_out = ((ch & 0x04) == 0x04); lcd_DB5_out = ((ch & 0x02) == 0x02); lcd_DB4_out = ((ch & 0x01) == 0x01); delay_1us(); delay_1us(); lcd_E = 1; delay_1us(); delay_1us(); lcd_E = 0; delay_10us(5); }
void Send_LenStop(USART_TypeDef *USARTx ) { u16 i; for (i=0;i<400;i++) { // USARTx->DR=(u8)0x00; USART2_SEND_DATA[0]=0; DMA_Cmd(DMA1_Channel7, DISABLE); //改变datasize前先要禁止通道工作 DMA1_Channel7->CNDTR=1; //DMA1,传输数据量 USART2_TX_Finish=0;//DMA传输开始标志量 DMA_Cmd(DMA1_Channel7, ENABLE); delay_10us(25);// 3515 } }
void wczytaj_haslo(int indeks) { unsigned char Tekst[2]; int i=0; char tab[5]={0,0,0,0,0}; Tekst[0]=0; Tekst[1]=0; while (i<4) { Tekst[0]=KBD_ReadKey(); if (Tekst[0] !=0 ) { tab[i]=Tekst[0]; i++; wyswietl_gwiazdki(i); Tekst[0]=0; } delay_10us(); } tab[4]=0; i=0; int dobrze=0; char dane=0; uint16_t adres_hasla=(adres(indeks)+4); for(;i<4;i++) { dane=(Read_24Cxx(adres_hasla,M2404)); if(tab[i]==dane) { dobrze++; } adres_hasla++; } LCD_WriteCommand(HD44780_CLEAR); LCD_GoTo(0,0); if(dobrze==4) { LCD_WriteText((unsigned char *)"Otwieram drzwi"); Zamek1_On(); } else { LCD_WriteText((unsigned char *)"Bledne haslo"); } }
void lcd_init(void) { /* 液晶初始化函数 参数: 空 返回: 空 调用: delay100us()、lcd_write_cmd() 说明: 重启ST7920、复位,设置功能,清屏 */ //功能设置 lcd_expand_switch(0); //8BIT控制界面,基本指令集 delay_10us(1); lcd_write_cmd(0x0c); //开显示、关光标,不闪烁 delay_10us(1); lcd_write_cmd(LCD_CMD_CLR); //清除屏幕显示,将DDRAM的地址计数器清零 delay_10us(1); lcd_write_cmd(0x06); //DDRAM的地址计数器(AC)加1 lcd_clr_pic(); delay_10us(1); }
BYTE LCD_get_BF(void) { BYTE low,high; lcd_DB4_tris = 1; lcd_DB5_tris = 1; lcd_DB6_tris = 1; lcd_DB7_tris = 1; lcd_RS = 0; lcd_RW = 1; delay_1us(); lcd_E = 1; delay_1us(); delay_1us(); high = ((lcd_DB7_in << 3) | (lcd_DB6_in << 2) | (lcd_DB5_in << 1) | lcd_DB4_in); lcd_E = 0; delay_1us(); delay_1us(); lcd_E = 1; delay_1us(); delay_1us(); low = ((lcd_DB7_in << 3) | (lcd_DB6_in << 2) | (lcd_DB5_in << 1) | lcd_DB4_in); lcd_E = 0; delay_1us(); delay_1us(); lcd_DB4_tris = 0; lcd_DB5_tris = 0; lcd_DB6_tris = 0; lcd_DB7_tris = 0; delay_10us(5); return ((high << 4) | low); }
void lcd_wait_busy(void) {/* 液晶等忙函数 说明: RS置零、RW置位,EA下降沿之后读忙位,BF为0则空闲 unsigned char busy; LCD_DB_BUS=0xff; LCD_RS = 0; LCD_RW = 1; LCD_EA=1; while(LCD_BUSY); LCD_EA=0; */ delay_10us(1); }
void init_lcd() { TRISBbits.RB7 = 0; TRISBbits.RB6 = 0; TRISBbits.RB5 = 0; TRISAbits.RA0 = 0; TRISAbits.RA1 = 0; TRISAbits.RA2 = 0; TRISAbits.RA3 = 0; RS = 0; RW = 0; E = 0; delay_s(1); nibble(0x03); __delay_ms(5); nibble(0x03); __delay_ms(5); nibble(0x03); delay_10us(15); nibble(0x02); send_command(LCD_FUNCTIONSET | LCD_4BITMODE | LCD_2LINE | LCD_5x8DOTS); send_command(LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF); TRISCbits.RC0 = 0; backlight_on(); clear(); home(); splash(SPLASH_MSG); clear(); home(); backlight_off(); }
int wczytaj_id() { unsigned char Tekst[2]; int i=0; char tab[5]={0,0,0,0,0}; Tekst[0]=0; Tekst[1]=0; while (i<4) { Tekst[0]=KBD_ReadKey(); if (Tekst[0] !=0 ) { tab[i]=Tekst[0]; i++; wyswietl_gwiazdki(i); Tekst[0]=0; } delay_10us(); } tab[4]=0; return (atoi((const char *)tab)); }
/** * @brief main entry point. * @par Parameters None * @retval void None * @par Required preconditions: None */ void main(void) { uint8_t PayloadLength, data_sensor, *bufMessage; /* deinit I/O ports */ DeInitClock(); DeInitGPIO(); /* Select HSI as system clock source */ #ifdef USE_HSI CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); #else CLK_SYSCLKSourceSwitchCmd(ENABLE); /* Select 2MHz HSE as system clock source */ CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_4); CLK_HSICmd(DISABLE); #endif // Initializes the LCD glass LCD_GLASS_Init(); /* LED button init: GPIO set in push pull */ GPIO_Init( LED_GPIO_PORT, LED_GPIO_PIN, GPIO_Mode_Out_PP_Low_Fast); // set to 0 GPIOE->ODR &= ~LED_GPIO_PIN; /* USER button init: GPIO set in input interrupt active mode */ GPIO_Init( BUTTON_GPIO_PORT, USER_GPIO_PIN, GPIO_Mode_In_FL_IT); EXTI_SetPinSensitivity(EXTI_Pin_7, EXTI_Trigger_Falling); //* Init Bar on LCD all are OFF BAR0_OFF; BAR1_OFF; BAR2_OFF; BAR3_OFF; enableInterrupts(); //* At power on VDD diplays bufMessage = NDEFmessage; if (EEMenuState > STATE_TEMPMEAS) EEMenuState = STATE_CHECKNDEFMESSAGE; FLASH_Unlock(FLASH_MemType_Data ); state_machine = EEMenuState ; delayLFO_ms (1); if (EEInitial == 0) { User_WriteFirmwareVersion (); EEInitial =1; } while (1) { switch (state_machine) { case STATE_VREFF: // measure the voltage available at the output of the M24LR04E-R Vref_measure(); delayLFO_ms (2); //turn on led GPIO_SetBits(GPIOE, GPIO_Pin_6); break; case STATE_VBIO: //measure the output voltage of biosensor through Pin 7 Port E CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_VrefintCmd(ENABLE); delay_10us(3); GPIO_DeInit(GPIOE); GPIO_Init(GPIOE,GPIO_Pin_7 ,/*GPIO_Mode_In_FL_No_IT*/GPIO_Mode_In_PU_No_IT); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single,ADC_Resolution_12Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_3, ENABLE); delay_10us(3); // Important delay res = 0; res_2 = 0; i=0; for(i=8; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* de-initialize ADC*/ ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ ADC_SchmittTriggerConfig(ADC1, ADC_Channel_3, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_3, DISABLE); res = res>>3; P_VREFINT_Factory = VREFINT_Factory_CONV_ADDRESS; #ifdef VREFINT_FACTORY_CONV if ((*P_VREFINT_Factory>VREFINT_Factory_CONV_MIN ) && (*P_VREFINT_Factory<VREFINT_Factory_CONV_MAX )) { /* If the value exists: Adds the hight byte to FullVREF_FACTORY */ FullVREF_FACTORY = VREFINT_Factory_CONV_MSB; FullVREF_FACTORY += *P_VREFINT_Factory; res_2 = (float)(FullVREF_FACTORY*VDD_FACTORY); res_2 /= res; } else { res_2 = (VREF/res) * ADC_CONV; // usally res>>3 } #else /* We use the theorcial value */ res_2 = (VREF/res) * ADC_CONV; #endif /* Vdd_appli in mV */ res_2*= 1000L; convert_into_char (res_2, tab); /* To add unit and decimal point */ tab[5] = 'V'; tab[4] = ' '; tab[1] |= DOT; /* To add decimal point for display in volt */ tab[0] = ' '; LCD_GLASS_DisplayStrDeci(tab); delayLFO_ms (2); //LCD_GLASS_DisplayString("V BIO"); break; case STATE_CHECKNDEFMESSAGE: // read the NDEF message from the M24LR04E-R EEPROM and display it if it is found if (User_ReadNDEFMessage (&PayloadLength) == SUCCESS) User_DisplayMessage (bufMessage,PayloadLength); // User_DisplayMessageActiveHaltMode (PayloadLength); else User_DisplayMessage(ErrorMessage,20); break; case STATE_TEMPMEAS: // read the ambiant tempserature from the STTS751 User_GetOneTemperature (&data_sensor); // display the temperature User_DisplayOneTemperature (data_sensor); delayLFO_ms (2); break; break; /* for safe: normaly never reaches */ default: LCD_GLASS_Clear(); LCD_GLASS_DisplayString("Error"); state_machine = STATE_VREFF; break; } } }
/** * @brief Current measurement in different MCU modes: RUN/HALT/LowPower withouto LCD/LowPower with LCD * @caller main and ADC_Icc_Test * @param MCU state * @retval ADC value. */ u16 ADC_Icc_Test(u8 Mcu_State) { uint16_t res; uint8_t i; /* Test MCU state for configuration */ switch (Mcu_State) { /* test Run mode nothing to do */ case MCU_RUN: break; /* Low power mode */ case MCU_LPR: Halt_Init(); sim(); /* To prepare to start counter */ GPIO_HIGH(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* To configure Low Power */ LPR_init(); break; /* Halt mode */ case MCU_HALT: /* Init for Halt mode */ Halt_Init(); sim(); /* To prepare to start counter */ GPIO_HIGH(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* Falling edge for start counter */ GPIO_LOW(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); delay_10us(1); /* MCU in halt during measurement */ /* Wake up by Interrupt done counter Input Port E pin 6 */ halt(); break; case MCU_LPR_LCD: PWR->CSR2 = 0x2; sim(); /* To configure GPIO for reduce current. */ GPIO_LowPower_Config(); /* To prepare to start counter */ GPIO_HIGH(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); /* To configure Low Power */ LPR_init(); break; } sim(); /* re-start ADC chanel 24 for Current measurement */ ADC_Icc_Init(); /* Read ADC for current measurmeent */ /* initialize result */ res = 0; for(i=8; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* ICC_CNT_EN invalid */ GPIO_HIGH(CTN_GPIO_PORT,CTN_CNTEN_GPIO_PIN); GPIO_Init(BUTTON_GPIO_PORT, USER_GPIO_PIN,GPIO_Mode_In_FL_IT); rim(); /* Disable ADC 1 for reduce current */ ADC_Cmd(ADC1, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); if (Mcu_State !=MCU_LPR_LCD) { CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE); LCD_GLASS_Init(); } return (res>>3); }
/******************************************************************************* * MAIN FUNCTION * *******************************************************************************/ int main(void) { unsigned char angle = 0; // declare a variable to store angle unsigned char i = 0,j = 0; unsigned char lx = 0, ly = 0, ry = 0; unsigned int rx = 0; // ensure all the hardware port in zero initially PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; // Initialize the I/O port direction, this must be configured according to circuit // please refer to PTK40A schematic for details // TRISX control pin direction, output pin must be configure as '0' // while input must be configure as '1' TRISA = 0b00010001; TRISB = 0b00001111; TRISC = 0b10010011; TRISD = 0; TRISE = 0; // Initialize ADC. adc_initialize(); //Ensure pin share with analog is being configured to digital correctly pwm_initialize(); uart_initialize(); beep(2); //buzzer sound for twice // PTK40A come SKPS PORT TO USE SKPS // SKPS, control DC motor speed and RC servo motor // Please connect SKPS at the "Cytron Starter Kit" section // Select 9600 as the baud rate on SKPS // servo require the PCM at around 20ms period // Please move jumper JP9 to SERVO, JP10 to PWM, JP20 & 21 to DC MOTOR M1 = 1; M2 = 0; // drive motor in a direction while(1) // create an infinite loop { lx = uc_skps(p_joy_lx); //obtain left joystick x axis value ly = uc_skps(p_joy_ly); //obtain left joystick y axis value rx = uc_skps(p_joy_rx); //obtain right joystick x axis value ry = uc_skps(p_joy_ry); //obtain right joystick y axis value //control dc motor using joyleft if(lx==128 && ly==128) { pwm_set_duty_cycle(0); } //if(lx!=128 && ly!=128) else if(ly==0) { pwm_set_duty_cycle(1000); //set dc motor speed in differect coordinate } else if(lx==0) { pwm_set_duty_cycle(350); } else if(ly==255) { pwm_set_duty_cycle(500); } else if(lx==255) { pwm_set_duty_cycle(750); } //control servo motor using joyright if(ry==0) { SERVO = 1; // Servo pin HIGH delay_10us(100); SERVO = 0; // Servo pin LOW delay_ms(18); // delay for around 18ms } else if(rx==0) { SERVO = 1; // Servo pin HIGH delay_10us(130); SERVO = 0; // Servo pin LOW delay_ms(18); // delay for around 18ms } else if(ry==255) { SERVO = 1; // Servo pin HIGH delay_10us(160); SERVO = 0; // Servo pin LOW delay_ms(18); // delay for around 18ms } else if(rx==255) { SERVO = 1; // Servo pin HIGH delay_10us(200); SERVO = 0; // Servo pin LOW delay_ms(18); // delay for around 18ms } } while(1) continue; // infinite loop to prevent PIC from reset if there is no more program }
void LCD_goto_xy(BYTE x, BYTE y) { LCD_write_byte(0, (0x80 | (((y > 1) ? 0x40 : 0) + (x - 1)))); delay_10us(5); }
void delay_10us_multiple(volatile unsigned char i) { for(i;i>0;i--) { delay_10us(); } }
void main() { unsigned char i; unsigned char savemode; // // Core Hardware Initialization mcu_initialize(); // Initialize MCU resources and 8KHz interrupt... nvreadbuf(); // Load the NV buffer from flash (get badge Addr and properties) proc_btn1(); // Process Buttons sound_config_polled(); // Configure the sound subsystem chip etoh_init(); // Initialize the Alcohol Sensor Subsystem MRF49XA_Init(); // Initialize the RF tranceiver usb_ser_init(); // Initialize the serial port light_init(); // // Preliminary setup and interaction // These setup a precurser items will occur prior to going into the main worker loop. // This is mainly the one-time powerup and "boot" sequence // // Show the address of the badge on the LEDs (since there are only 7 LED's // the binary number will show in green if the badge is < 128 and red if > // 128 with the badge addresses 0 and 128 showing up as nothing (typically illegal // numbers anyway // Note, led_showbin does not interact with the interrupt driven light show well. // So only use if you may not be in interrupt mode yet of if a show is not running delay_s(1); led_showbin(LED_SHOW_RED, nvget_badgetype() | 0x40); // Startup Indicator delay_10us(100); led_showbin(LED_SHOW_RED, 0); delay_s(1); led_showbin(LED_SHOW_RED, nvget_badgeperm() | 0x40); // Same delay_10us(100); led_showbin(LED_SHOW_RED, 0); delay_s(1); // // Shows address as described above // MyBadgeID = nvget_badgeid(); led_showbin(MyBadgeID & 0x80 ? LED_SHOW_RED : LED_SHOW_GRN, MyBadgeID & 0x7f ); delay_s(TIME_ADDR); led_showbin(LED_SHOW_NONE, 0); init_rnd(nvget_badgetype()+1, MyBadgeID, 0x55); // Seed tha random number generator // // Perform POV credits (spin the badge on your wrist :) // led_pov(LED_SHOW_AUTO, TIME_POV); // Enable Global Interrupts // Use Interrupts. ISR is interrupt(), below intcon.GIE=1; //sample_play(); //delay_s(2); MyMode = MODE_IDLE; modelights(); // Sets lightshow based on a few state parameters // Calculate initial elevation time elevatemsecs = elapsed_msecs + ELEVATE_BASE + (unsigned long) rnd_randomize() * ELEVATE_VAR; // // Main Worker Loop // while(1) { // // Handle the elapsed time clocks // clear_bit(intcon, TMR0IE); loop_msecs = intr_msecs; // Take copy to avoid race conditions intr_msecs = 0; set_bit(intcon, TMR0IE); elapsed_msecs += loop_msecs; // Update elapsed time in msecs savemode = MyMode; // Used to detact mode changes when command processers set new mode switch(MyMode) { case MODE_IDLE: // If button pressed, handle it if(btn_commandwork(loop_msecs) == BTN_WORKING) { MyMode = MODE_GETCMD; modelights(); playsong = 0; //light_pause(); break; } // Look for incoming RF packet if(MRF49XA_Receive_Packet(rfrxbuf,&rfrxlen) == PACKET_RECEIVED) { MRF49XA_Reset_Radio(); //led_showbin(LED_SHOW_RED, 2); //delay_10us(10); //led_showbin(LED_SHOW_RED, 0); rfcmd_execute(rfrxbuf, rfrxlen); } if(savemode != MyMode) { // Don't process remainder of idle switch if state change break; } // If time to send a beacon, send it. Also process quorum sounds. if(elapsed_msecs > (last_beacon + (unsigned long)BEACON_BASE + ((unsigned long)rnd_randomize()* BEACON_RNDSCALE))) { // time for beacon rfcmd_3send(RFCMD_BEACON, MyBadgeID, nvget_socvec1()); last_beacon = elapsed_msecs; rfcmd_clrcden(); if(have_quorum) { if(rfcmd_getdensity() <= DENSITY_QUORUM_LO) { have_quorum = 0; if(!playsong) tune_startsong(SONG_CHIRP2); } } else { if(rfcmd_getdensity() >= DENSITY_QUORUM_HI) { have_quorum = 1; if (!playsong) tune_startsong(SONG_303); } } modelights(); } break; case MODE_ETOH: // Run the alcohol sensor state machine if(etoh_breathtest(ETOH_DOWORK, loop_msecs ) == ETOH_DONE) { // worker for ETOH switch(etoh_getreward()) { case REWARD_SOBER: tune_startsong(SONG_BUZZER); break; case REWARD_TIPSY: tune_startsong(SONG_PEWPEW); break; case REWARD_DRUNK: tune_startsong(SONG_CACTUS); break; } MyMode = MODE_IDLE; light_show(LIGHTSHOW_SOCFLASH, 5); break; } break; case MODE_ATTEN: // Puts all badges into visual attention mode to see who's in range if(MyMode != lastmode) { attelapsed = 0; } attelapsed += loop_msecs; if(attelapsed > 120000) { // Stays there 2 min (should be in #def) MyMode = MODE_IDLE; } if(MRF49XA_Receive_Packet(rfrxbuf,&rfrxlen) == PACKET_RECEIVED) { MRF49XA_Reset_Radio(); rfcmd_execute(rfrxbuf, rfrxlen); } modelights(); break; case MODE_GETCMD: if (btn_commandwork( loop_msecs) != BTN_WORKING) { MyMode = MODE_IDLE; modelights(); } break; } // switch tune_songwork(); // Worker thread for songs light_animate(loop_msecs); // worker thread for lights if(MyElev) { // Handle the privilege elevation timer if(elapsed_msecs > (elevatemsecs + ELEVATE_DUR)) { MyElev = 0; elevatemsecs = elapsed_msecs + ELEVATE_BASE + (unsigned long) rnd_randomize() * ELEVATE_VAR; modelights(); } } else { if(elapsed_msecs > elevatemsecs) { MyElev = 1; sample_play(); modelights(); } } lastmode = MyMode; } }