Ejemplo n.º 1
0
void main()
{
#define ADDR    0x00
    u8 i = 0;
    u8 Data;
    uart_init(UART1, 19600);                        //初始化串口

    I2C_init(I2C0);                                 //初始化I2C0

    printf("AT24C02 I2C 实验\n\n");
    printf("\t\t——野火kinetis开发板\n\n");

    while(1)
    {
        for(i = 0; i < 255; i++)
        {
            I2C_WriteAddr(I2C0, AT24C02_I2C_ADDRESS, ADDR, i);          //I2C向AT24C02_I2C_ADDRESS芯片写入数据 i 到地址为ADDR的寄存器
            Data    =   I2C_ReadAddr(I2C0, AT24C02_I2C_ADDRESS, ADDR);  //I2C向AT24C02_I2C_ADDRESS芯片读取寄存器地址为ADDR的数据

            printf("接收到的数据为:%d\n\n", Data);                     //发送到串口显示出来

            time_delay_ms(1000);                                        //延时1s
        }
    }
#undef  ADDR
}
Ejemplo n.º 2
0
void main (void)
{
  signed char resultx, resulty, resultz;

  printf("Kinetis I2C Demo\n");

  //Initialize I2C
  init_I2C();

  //Configure MMA7660 sensor
  MMA7660WriteRegister(0x09,0xE0); //Disable tap detection
  MMA7660WriteRegister(0x07,0x19); //Enable auto-sleep, auto-wake, and put in active mode

  printf("  X     Y     Z\n");

  while(1)
  {
    //Read x-axis register
    resultx = u8MMA7660ReadRegister(0x00);
    printf("%3d", convert(resultx));

    //Read y-axis register
    resulty = u8MMA7660ReadRegister(0x01);
    printf("   %3d", convert(resulty));

    //Read z-axis register
    resultz = u8MMA7660ReadRegister(0x02);
    printf("   %3d\n", convert(resultz));

    //Delay for 250ms
    time_delay_ms(250);
  }

}
Ejemplo n.º 3
0
void main()
{
#define ADDR    0x00
    u8 i = 0;
    u8 Data;
    uart_init(UART1, 19600);                    //初始化串口

    AT24C02_init();                             //初始化AT24C02,启动I2C总线

    printf("AT24C02 I2C 实验\n\n");
    printf("\t\t——野火kinetis开发板\n\n");
    while(1)
    {
        for(i = 0; i < 255; i++)
        {
            AT24C02_WriteByte(ADDR, i);         //向地址ADDR写入数据i

            Data = AT24C02_ReadByte(ADDR);      //读取地址ADDR的数据

            printf("接收到的数据为:%d\n\n", Data); //发送到串口显示出来

            time_delay_ms(1000);                //延时1s
        }
    }
#undef  ADDR
}
Ejemplo n.º 4
0
Archivo: key.c Proyecto: Magicwangs/IAR
void Key_Event(void)
{     
    u8 tmp;
    
    
    PTE_BYTE0_OUT &= 0x00;              // 设置P1OUT全为0,等待按键输入
    tmp = keyin;                 // 获取 p1IN
    if ((key_Pressed == 0x00)&&((tmp & 0x0f) < 0x0f)) //如果有键按下
    {                     
        key_Pressed = 1;        // 如果有按键按下,设置key_Pressed标识
        time_delay_ms(15) ;             //消除抖动
        Check_Key();            // 调用check_Key(),获取键值 
    }     
    else if ((key_Pressed == 1)&&((tmp & 0x0f) == 0x0f)) //如果按键已经释放
    {                     
        key_Pressed = 0;        // 清除key_Pressed标识
        key_Flag    = 1;        // 设置key_Flag标识
       
        
        switch(key_val)
        {
        case 1:I_Set+=0.1;break;
        case 4:I_Set-=0.1;break;
          
        case 2:Kp_i+=1;break;
        case 5:Kp_i-=1;break;
        
        case 3:Ki_i+=0.01;break;
        case 6:Ki_i-=0.01;break;
        
        case 7:U20+=0.1;break;
        case 14:U20-=0.1;break; 
        
        case 8:Kp_u+=1;break;
        case 0:Kp_u-=1;break;
        
        case 9:Ki_u+=0.01;break;
        case 15:Ki_u-=0.01;break;
        
        case 10:I_Set=1.0;U20=29.0;Kp_i=20.0;Ki_i=0.3;Kp_u=20.0;Ki_u=0.5;break;
        
        case 13:start_flag=1;test_flag=0;break;
        case 12:modeflag=1;break;
        
        case 11:test_flag=1;break;
        default:break;
        
        }
        
        key_val=11;
        
        Refresh_Key();
    }
    else 
    {
        asm("nop");
    }

}
Ejemplo n.º 5
0
/*!
 * Read first three registers from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned long u8MMA7660ReadThreeRegisters(unsigned char u8RegisterAddress)
{
  unsigned char result1 = 0, result2 = 0, result3 = 0;

  /* Send Slave Address */
  IIC_StartTransmission(SlaveID,MWSR);
  i2c_Wait();

  /* Write Register Address */
  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  /* Do a repeated start */
  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */
  I2C0_D = (MMA7660_I2C_ADDRESS << 1) | 0x01; //read address
  i2c_Wait();

  /* Put in Rx Mode */
  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Ensure TXAK bit is 0 */
  I2C0_C1 &= ~I2C_C1_TXAK_MASK;

  /* Dummy read */
  result1 = I2C0_D ;
  i2c_Wait();

  /* Read first byte */
  result1 = I2C0_D;
  i2c_Wait();

  /* Turn off ACK since this is second to last read*/
  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Read second byte */
  result2 = I2C0_D;
  i2c_Wait();

  /* Send stop */
  i2c_Stop();

  /* Read third byte */
  result3 = I2C0_D;
  

  unsigned long totalresult = result1;
  totalresult <<= 8;
  totalresult |= result2;
  totalresult <<= 8;
  totalresult |= result3;
  
  // Provide a gap between this read and the next
  time_delay_ms(250);
  
  return totalresult;
}
Ejemplo n.º 6
0
int main(void)
{
	SIM_COPC = 0x00;		//Deshabilito el watchdog
	CLK_init();				//Activo relojes de PORTA y PORTB, Core Clock = 47.972.352 Hz, Bus Clock = 23.986.176 Hz

	GPIO_Init(PORT_A, 10, IO_MUX);	//Selecciono MUX de IO para PTA10
	GPIO_IO(PORT_A, 10, OUTPUT);	//Configuro PTA10 como salida

    for (;;) {

    	GPIO_Set(PORT_A, 10);		//PTA10 = 1
    	time_delay_ms(1000);		//Espero 1 segundo
    	GPIO_Clear(PORT_A, 10);		//PTA10 = 0
    	time_delay_ms(1000);		//Espero 1 segundo
    }
    /* Never leave main */
    return 0;
}
Ejemplo n.º 7
0
void Sleep( double sec )
{
#ifdef __USE_NEW_SLEEP__
    const unsigned int tsr0 = RTC_TSR;
    const unsigned int tpr0 = RTC_TPR;
    Delay( (unsigned int)( sec * 1000 ), tsr0,tpr0 );
#else
    time_delay_ms( (unsigned int)( sec * 1000 ) );
#endif
}
Ejemplo n.º 8
0
void Sleep( int msec )
{
#ifdef __USE_NEW_SLEEP__
    const unsigned int tsr0 = RTC_TSR;
    const unsigned int tpr0 = RTC_TPR;
    Delay( (unsigned int)msec, tsr0,tpr0 );
#else
    time_delay_ms( (unsigned int)msec );
#endif
}
Ejemplo n.º 9
0
void LCD_init(char mode1, char mode2)
{
	char aux;
    //SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK;       // enable clock to Port A and Port B
    PORTA_PCR8 = PORT_PCR_MUX(1);		// PTA8 GPIO
    PORTA_PCR9 = PORT_PCR_MUX(1);		// PTA9 GPIO
    PORTA_PCR11 = PORT_PCR_MUX(1);		// PTA11 GPIO
    PORTB_PCR4 = PORT_PCR_MUX(1);		// PTB4 GPIO
    PORTA_PCR12 = PORT_PCR_MUX(1);		// PTA12 GPIO
    PORTB_PCR12 = PORT_PCR_MUX(1);		// PTB12 GPIO
    //PORTA_PCR13 = PORT_PCR_MUX(1);		// PTA13 GPIO
    GPIOA_PDDR |= LCD_ENABLE|LCD_RS|LCD_D4|LCD_D6/*|LCD_D7*/;
    GPIOB_PDDR |= LCD_D5|LCD_D7;

	// Set the LCD data pins to zero
	LCD_ENABLE_OFF;
	LCD_RS_OFF;
	LCD_D4_OFF;
	LCD_D5_OFF;
	LCD_D6_OFF;
	LCD_D7_OFF;

	time_delay_ms(50);			//Secuencia de inicializacion
    
	// LCD 4-bit mode initialization sequence
	// send three times 0x03 and then 0x02 to finish configuring the LCD
	for(aux=0;aux<3;++aux)
	{		
	  LCD_send_nibble(3);
	  time_delay_ms(5);
	}
	LCD_send_nibble(2);
	// Now send the LCD configuration data
	LCD_send_byte(0,0x20 | mode1);
	LCD_send_byte(0,0x08 | mode2);
	lcd_mode = 0x08 | mode2;
	LCD_send_byte(0,1);
	LCD_send_byte(0,6);
}
Ejemplo n.º 10
0
/*************************************************************************
*                             野火嵌入式开发工作室
*                               PWM实验LED测试
*
*  实验说明:野火PWM实验,用LED来测试占空比的变化。
*
*  实验操作:这里用 FTM1_CH0 产生 PWM 脉冲波
*            在 FTM.h 里,可以查到 FTM1_CH0 对应管脚为 PTA8
*            把 PTA8 接入 LED0,即 PTA8 和 PTD15 短接
*
*  实验效果:可以看到 LED0 由暗变亮,再突然暗,如此下去……
*
*  实验目的:测试 PWM 频率是否正确
*
*  修改时间:2012-2-29     已测试
*
*  备    注:野火Kinetis开发板的 LED0~3 ,分别接PTD15~PTD12 ,低电平点亮
*            FTM.h 里有各个FTM通道所对应管脚的表格,方便查看
*            占空比传递进入的参数,要根据 FTM_PRECISON 的定义来选择
*************************************************************************/
void  main()
{
    u32 i;
    FTM_PWM_init(FTM1, CH0, 35000, 100);     //FTM1_CH0初始化PWM :PA8
    while(1)
    {
        for(i = 10; i > 1; i--)
        {
            FTM_PWM_Duty(FTM1, CH0, i * 10);  //改变占空比,逐渐变小 ,LED 逐渐变亮 (低电平点亮)
            time_delay_ms(100);               //延时100ms
        }
    }
}
Ejemplo n.º 11
0
//**************************************************************************
//* Write a byte into the LCD
//**************************************************************************
//* Calling arguments:
//* char address : 0 for instructions, 1 for data
//* char data : command or data to be written
//**************************************************************************
void LCD_send_byte(char address, char data)
{
	if (address == 1)
	{
		LCD_RS_ON;
	} else
	{
		LCD_RS_OFF;
	}
	LCD_ENABLE_OFF;                 // set LCD enable line to 0
	LCD_send_nibble(data >> 4);     // send the higher nibble
	LCD_send_nibble(data & 0x0f);   // send the lower nibble
	time_delay_ms(6);
}
Ejemplo n.º 12
0
int main(void)
{
  clock_init();
  time_init();
  pwm_init(0);
  led_green_init();

  while (1) {
    time_delay_ms(500);
    led_green_toggle();
  }

  return 0;
}
Ejemplo n.º 13
0
void main (void)
{
  char ones, tens, hundreds;
  unsigned int counter=0;


  /* Initialize TWRPI-SLCD on TWR-K40X256 board*/
  init_twrpi_lcd();

  tlcd_all_on(); //Turn on all segments
  time_delay_ms(5000); //Delay so can see all segments on

  tlcd_all_off(); //Turn off all segments
  tlcd_fsl_on();  //Turn on FSL segment

  /* Increment counter and display value on SLCD */
  while(1)
  {
    //Parse counter values to get each digit
    ones=counter%10;
    tens=(counter/10)%10;
    hundreds=(counter/100)%10;

    //Update SLCD
    tlcd_set_numeric3(ones);
    tlcd_set_numeric2(tens);
    tlcd_set_numeric1(hundreds);

    //Increment counter and keep below 1000
    counter++;
    counter=counter%1000;

    //Delay for 250ms
    time_delay_ms(250);
  }

}
Ejemplo n.º 14
0
/*
 * To read data from a LCD register, this function sets the register index,
 * and then sends out 0x73 and wait for reply
 */
static unsigned short SpiReadDataWord(unsigned char reg)
{
  unsigned short value, data;
  unsigned short data2;

  //Set register pointer
  data=0x7000 | reg;

  // wait write buffer not full flag
  while (!(SPI2_SR & SPI_SR_TFFF_MASK)){};

  // Assert CS0, Use config 0
  SPI2_PUSHR = SPI_PUSHR_PCS(1 << (0)) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_TXDATA((unsigned short)data);

  while (!(SPI2_SR & SPI_SR_TCF_MASK)){};// while shift-out complete
  SPI2_SR = SPI_SR_TCF_MASK;           // clear flag


  //Tell it to do a read by sending out 0x73
  data=0x7300;
  data2=0x0000;

  //Halt SPI from sending out anything
  SPI2_MCR  |= SPI_MCR_HALT_MASK;

  // wait write buffer not full flag
  while (!(SPI2_SR & SPI_SR_TFFF_MASK)){};

  // Assert CS0, Use config 0, write two words at once to have CW low long enough
  //  to let data be recieved.
  SPI2_PUSHR = SPI_PUSHR_CONT_MASK | SPI_PUSHR_PCS(1 << (0)) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_TXDATA((unsigned short)data);
  SPI2_PUSHR = SPI_PUSHR_PCS(1 << (0)) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_TXDATA((unsigned short)data2);


  //Send out both data words back to back using the FIFO
  SPI2_MCR &= (~ SPI_MCR_HALT_MASK);

  while (!(SPI2_SR & SPI_SR_TCF_MASK)){};// while shift-out complete
  //while (!(SPI2_SR & SPI_SR_RCF_MASK)){};// while shift-out complete
  SPI2_SR = SPI_SR_TCF_MASK;           // clear flag

  time_delay_ms(5);

  //Data returned from LCD should be in POPR now
  value = SPI2_POPR ;  //garbage
  value = SPI2_POPR ;  //good data

  return value;
}
Ejemplo n.º 15
0
/*!
 * Read a register from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char u8MMA7660ReadRegister(unsigned char u8RegisterAddress)
{
  signed char result = 0;
  int tries_remaining = 5;
  
//  while ((result & 0x40) && tries_remaining--)
//  {
    /* Send Slave Address */
    IIC_StartTransmission(SlaveID,MWSR);
    i2c_Wait();
  
    /* Write Register Address */
    I2C0_D = u8RegisterAddress;
    i2c_Wait();
  
    /* Do a repeated start */
    I2C0_C1 |= I2C_C1_RSTA_MASK;
  
    /* Send Slave Address */
    I2C0_D = (MMA7660_I2C_ADDRESS << 1) | 0x01; //read address
    i2c_Wait();
  
    /* Put in Rx Mode */
    I2C0_C1 &= (~I2C_C1_TX_MASK);
  
    /* Turn off ACK since this is second to last byte being read*/
    I2C0_C1 |= I2C_C1_TXAK_MASK;
  
    /* Dummy read */
    result = I2C0_D ;
    i2c_Wait();
  
    /* Send stop since about to read last byte */
    i2c_Stop();
  
    /* Read byte */
    result = I2C0_D ;
 // }
  
  if (result & 0x40)
  {
    puts("Returning alerted (invalid) result");
  }

  time_delay_ms(200);
  return result;
}
Ejemplo n.º 16
0
//**************************************************************************
//* Write a character on the display
//**************************************************************************
//* Calling arguments:
//* char c : character to be written
//**************************************************************************
//* Notes :
//* \f clear the display
//* \n and \r return the cursor to line 1 column 0
//**************************************************************************
void LCD_write_char(char c)
{
  switch (c)
	{
	  case '\f' :	
	    LCD_send_byte(0,1);
	    time_delay_ms(5);
	    break;
	  case '\n' :
	  case '\r' :	
	    LCD_pos_xy(0,1);
	    break;
	  case RightShiftCmd :
		LCD_send_byte(0,c);
		break;
	  default:
	    LCD_send_byte(1,c);
   }
}
Ejemplo n.º 17
0
unsigned char u8OpCheckSpo2 (void)
{
  unsigned short u16AdcRead = 0;
  
  MEDCON_INIT();                                                                //Initialize Medical Connector
  
  ADC1_Init16b();								//Initialize ADC1 so we can read TRIAMP1 Out
  MEDCON_PWRON();								//Turn on medical connector
  
  time_delay_ms(100);                                                           //Wait 100mS for signal stabilization

  u16AdcRead = ADC1_Read16b(22);						//Read TRIAMP1 Out (ADC1_SE22)
  
  if(u16AdcRead >= (_1_mV * 1500) && u16AdcRead <= (_1_mV * 1800))	        //Check if TRIAMP1_OUT = VCC/2
  {
          return OPAMPS_PRESENT;						//TRIAMP1_OUT = VCC/2 OpAmps present
  }
  else
  {
          return OPAMPS_NOT_PRESENT;						//OpAmps not present
  }

}
Ejemplo n.º 18
0
/*****************************************************************************
* Functions
*****************************************************************************/
unsigned char u8OpCheckGlu (void)
{
 unsigned short u16AdcRead = 0;
 
 MEDCON_INIT();                                                                 //Initialize Medical Connector
 
 ADC0_Init16b();								//Initialize ADC1 in order to read OpAmp0 Out
 MEDCON_PWRON();								//Turn on medical connector

 time_delay_ms(100);                                                            //Wait 100mS for signal stabilization
 
 u16AdcRead = ADC0_Read16b(0);							//Read ADC0_CH0 (OpAmp0 Out)

 if(u16AdcRead >= (_1_mV*300) && u16AdcRead <= (_1_mV*700))			//Check if ADC0 is into the range (300mV-700mV)
 {
 	return OPAMPS_PRESENT;							//Baseline present, OpAmps are populated 
 }
 else
 { 
	return OPAMPS_NOT_PRESENT;						//No baseline present, OpAmps are not populated
 } 

}
Ejemplo n.º 19
0
/*
 * SPI configuration to initialize the LCD
 */
static void lcdc_init_spi_lcd(void)
{
  spi_init();

  time_delay_ms(50);

  SpiRegSet(0x20);

  SpiRegSet(0x36);
  SpiSendDataWord(0x00);

  SpiRegSet(0x3A);
  SpiSendDataWord(0x70); // 24bit mode

  SpiRegSet(0xB1);
  SpiSendDataWord(0x06);
  SpiSendDataWord(0x43);
  SpiSendDataWord(0x0A);


  SpiRegSet(0xB2);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0xC8);

  SpiRegSet(0xB3);
  SpiSendDataWord(0x00);

  SpiRegSet(0xB4);
  SpiSendDataWord(0x04);

  SpiRegSet(0xB5);
  SpiSendDataWord(0x42);
  SpiSendDataWord(0x10);
  SpiSendDataWord(0x10);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);

  SpiRegSet(0xB6);
  SpiSendDataWord(0x0B);
  SpiSendDataWord(0x18);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x40);
  SpiSendDataWord(0x10);
  SpiSendDataWord(0x30);

  SpiRegSet(0xC3);
  SpiSendDataWord(0x07);
  SpiSendDataWord(0x04);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x04);

  time_delay_ms(500);

  SpiRegSet(0xC4);
  SpiSendDataWord(0x12);
  SpiSendDataWord(0x24);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x49);

  time_delay_ms(500);

  SpiRegSet(0xC5);
  SpiSendDataWord(0x5B);

  time_delay_ms(500);

  SpiRegSet(0xC6);
  SpiSendDataWord(0x44);
  SpiSendDataWord(0x63);

  time_delay_ms(500);

  SpiRegSet(0xF9);
  SpiSendDataWord(0x40);

  SpiRegSet(0xD0);
  SpiSendDataWord(0x01);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x73);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x02);

  SpiRegSet(0xD1);
  SpiSendDataWord(0x01);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x73);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x02);

  SpiRegSet(0xD2);
  SpiSendDataWord(0x01);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x73);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x02);

  SpiRegSet(0xD3);
  SpiSendDataWord(0x01);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x73);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x02);

  SpiRegSet(0xD4);
  SpiSendDataWord(0x01);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x73);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x02);

  SpiRegSet(0xD5);
  SpiSendDataWord(0x01);
  SpiSendDataWord(0x17);
  SpiSendDataWord(0x73);
  SpiSendDataWord(0x02);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x00);
  SpiSendDataWord(0x02);

  SpiRegSet(0x11);

  time_delay_ms(120);

  SpiRegSet(0x29);

  printf("Finished LCD SPI Init\n");
}