/*******************************************************************************
* Function Name  : NRF24L01_Init
* Description    : 初始化24L01的IO口
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void NRF24L01_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /*config CE CSN*/
  RCC_APB2PeriphClockCmd(RCC_NRF24L01_CE, ENABLE);           //使能GPIO的时钟
  GPIO_InitStructure.GPIO_Pin = NRF24L01_CE_PIN;              //NRF24L01 模块片选信号
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIO_NRF24L01_CE, &GPIO_InitStructure);

  RCC_APB2PeriphClockCmd(RCC_NRF24L01_CSN, ENABLE);          //使能GPIO的时钟
  GPIO_InitStructure.GPIO_Pin = NRF24L01_CSN_PIN;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIO_NRF24L01_CSN, &GPIO_InitStructure);

  Set_NRF24L01_CE;                                           //初始化时先拉高
  Set_NRF24L01_CSN;                                   //初始化时先拉高

  /*config irq*/
  GPIO_InitStructure.GPIO_Pin = NRF24L01_IRQ_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU  ;     //上拉输入
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIO_NRF24L01_IRQ, &GPIO_InitStructure);
  GPIO_SetBits(GPIO_NRF24L01_IRQ,NRF24L01_IRQ_PIN);

  SPI1_Init();                                       //初始化SPI
  Clr_NRF24L01_CE; 	                               //使能24L01
  Set_NRF24L01_CSN;                                  //SPI片选取消
}
Esempio n. 2
0
// Main
void main (void)
{
	// Init the system clock
	SystemInitClock();
	
	// Enable the peripherals
	SystemEnablePeripheral(PERIPHERAL_AFIO);
	SystemEnablePeripheral(PERIPHERAL_IOPA);
	SystemEnablePeripheral(PERIPHERAL_IOPB);
	SystemEnablePeripheral(PERIPHERAL_IOPC);
	SystemEnablePeripheral(PERIPHERAL_IOPD);
	SystemEnablePeripheral(PERIPHERAL_IOPE);
	SystemEnablePeripheral(PERIPHERAL_DMA1);

	// Init the FLASH chip and SPI module
	// Init the ENC chip and SPI module
	FLASH_Init();
	SPI1_Init();
	SPI2_Init();
	ENC_Init();

	// Init kernel
	KERNEL_Init();

	// Create tasks
	KERNEL_AddTask(&TaskDescriptor_TaskENC);

	// Start kernel (this function never returns)
	KERNEL_Start();
}
Esempio n. 3
0
/*----------------------------------------------------------*\
 | MIAN ENTRY                                               |
\*----------------------------------------------------------*/
int main (void)
{
    int rev = 0;

  stm32_Init ();                                // STM32 setup
//  GPIOD->ODR &= ~(1<<9);//GPIOA->BRR = ENC28J60_CS;
//  GPIOD->ODR |= 1<<9;//GPIOA->BSRR = ENC28J60_CS;
  printf ("SPI1_Init starting...\r\n");
  SPI1_Init();
  printf ("enc28j60 init...\r\n");
  //enc28j60Init((unsigned char *)enc28j60_MAC);  


    simple_server();

    enc28j60Init((unsigned char *)enc28j60_MAC);

    rev = enc28j60getrev();

    return rev;

/*
  for(;;) {
    unsigned char c;

    printf ("Press a key. ");
    c = getchar ();
    printf ("\r\n");
    printf ("You pressed '%c'.\r\n\r\n", c);
    }
*/
  }
Esempio n. 4
0
int simple_client(eAdrGD* fADRGD,uint8_t* fSostEth,uint8_t* nBlock, uint8_t* fIPAddr,uint8_t* fMACAddr,uint8_t* fPORTNUMBER)
	{
	SPI1_Init();
	pADRGD=fADRGD;
	EthSost=fSostEth;
	EthBlock=nBlock;
	PORTNUMBER=fPORTNUMBER;
	MACAddr=fMACAddr;
	IPAddr=fIPAddr;
	enc28j60Init(MACAddr);
	enc28j60PhyWrite(PHLCON,0x476);
	init_ip_arp_udp_tcp(MACAddr,fIPAddr,*PORTNUMBER);
	//dat_p=packetloop_arp_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf));

	buf[UDP_DATA_P]='H';
	buf[UDP_DATA_P+1]='E';
	buf[UDP_DATA_P+2]='L';
	buf[UDP_DATA_P+3]='L';
	buf[UDP_DATA_P+4]='O';
	buf[UDP_DATA_P+5]=' ';
	buf[UDP_DATA_P+6]='W';
	buf[UDP_DATA_P+7]='O';
	buf[UDP_DATA_P+8]='R';
	buf[UDP_DATA_P+9]='L';
	buf[UDP_DATA_P+10]='D';

	//Sockets[0].IP_PHASE=0;

    //make_tcp_ack_from_any(fbuf); // send ack for http get
    //make_tcp_ack_with_data(fbuf,plen,0); // send data

	send_udp_prepare(buf,5001,dis_ip,5001,dis_mac);
	send_udp_transmit(buf,11);

	}
/**
  * @brief  SPI error treatment function.
  * @param  None
  * @retval None
  */
 void SPI1_Error (void)
{
  /* De-Initialize the SPI comunication BUS */
  HAL_SPI_DeInit(&SpiHandle);

  /* Re-Initiaize the SPI comunication BUS */
  SPI1_Init();
}
Esempio n. 6
0
void ADXL345_Init() {
    uint8_t test = 0;
    Timer_Init();
    
    ADXL345_AccelVCCInit();
    SPI1_Init();  
    TestLeds_GPIO_Init();
    //EXTI_Init();
    NSS_Low();

    test = ADXL345_read(DEVID_ADDRESS);
    if (test != DEVID) {
      GPIOE->BSRR |= (1 << TEST_LEDS[0]);
    } else {
      GPIOE->BSRR |= (1 << TEST_LEDS[0]) << 16;
    }

    ADXL345_write(INT_MAPPING_ADDRESS, DATA_READY_INT0_MAPPING);
    test = ADXL345_read(INT_MAPPING_ADDRESS);
    if (test != DATA_READY_INT0_MAPPING) {
      GPIOE->BSRR |= (1 << TEST_LEDS[1]);
    } else {
      GPIOE->BSRR |= (1 << TEST_LEDS[1]) << 16;
    }

    ADXL345_write(POWER_CTL_ADDRESS, MEASUREMENT_MODE);
    test = ADXL345_read(POWER_CTL_ADDRESS);
    if (test != MEASUREMENT_MODE) {
      GPIOE->BSRR |= (1 << TEST_LEDS[2]);
    } else {
      GPIOE->BSRR |= (1 << TEST_LEDS[2]) << 16;
    }

    ADXL345_write(DATA_FORMAT_ADDRESS, FULL_RES_MODE);
    test = ADXL345_read(DATA_FORMAT_ADDRESS);
    if (test != FULL_RES_MODE) {
      GPIOE->BSRR |= (1 << TEST_LEDS[3]);
    } else {
      GPIOE->BSRR |= (1 << TEST_LEDS[3]) << 16;
    }

    ADXL345_write(BW_RATE_ADDRESS, ACCEL_FREQ);
    test = ADXL345_read(BW_RATE_ADDRESS);
    if (test != ACCEL_FREQ) {
      GPIOE->BSRR |= (1 << TEST_LEDS[4]);
    } else {
      GPIOE->BSRR |= (1 << TEST_LEDS[4]) << 16;
    }

    ADXL345_write(INT_ENABLE_ADDRESS, DATA_READY_INT);
    test = ADXL345_read(INT_ENABLE_ADDRESS);
    if (test != DATA_READY_INT) {
      GPIOE->BSRR |= (1 << TEST_LEDS[5]);
    } else {
      GPIOE->BSRR |= (1 << TEST_LEDS[5]) << 16;
    }
    NSS_High();
}
Esempio n. 7
0
void Init_Peripherals(void)
{
 Init_Ports();
 Init_ADC_Pin();
 Init_ADC0();
 Init_ADC1();
Init_Sensor_Switch_Pin();
 SPI1_Init();
 Init_PWM();

}
Esempio n. 8
0
void Touch_Init(void)
{
  	GPIO_InitTypeDef GPIO_InitStructure;

  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//pen_int:PD6   
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);		   
	SPI1_Init();
	CSPin_init();
}
Esempio n. 9
0
//#=================================================================================================
//#
//#-------------------------------------------------------------------------------------------------
void mOLED_Init( void )
{
    _TRISA4 = 0;
    _TRISA0 = 0;
    _TRISB4 = 0;
    _TRISB1 = 0;
    SPI1_Init();

    rst_high;
    // VDD (3.3V) goes high at start, lets just chill for a ms
    Delayms(10);
    rst_low;
    // wait 10ms
    Delayms(20);
    rst_high;

    // Init sequence for 128x32 OLED module
    mOLED_SSD1306_cmd(SSD1306_DISPLAYOFF);                    // 0xAE
    mOLED_SSD1306_cmd(SSD1306_SETDISPLAYCLOCKDIV);            // 0xD5
    mOLED_SSD1306_cmd(0x80);                                  // the suggested ratio 0x80
    mOLED_SSD1306_cmd(SSD1306_SETMULTIPLEX);                  // 0xA8
    mOLED_SSD1306_cmd(0x1F);
    mOLED_SSD1306_cmd(SSD1306_SETDISPLAYOFFSET);              // 0xD3
    mOLED_SSD1306_cmd(0x0);                                   // no offset
    mOLED_SSD1306_cmd(SSD1306_SETSTARTLINE | 0x0);            // line #0
    mOLED_SSD1306_cmd(SSD1306_CHARGEPUMP);                    // 0x8D

    //mOLED_SSD1306_cmd(0x10);    //SSD1306_EXTERNALVCC 0x1
    mOLED_SSD1306_cmd(0x14);    //SSD1306_SWITCHCAPVCC 0x2

    mOLED_SSD1306_cmd(SSD1306_MEMORYMODE);                    // 0x20
    mOLED_SSD1306_cmd(0x00);                                  // 0x0 act like ks0108
    mOLED_SSD1306_cmd(SSD1306_SEGREMAP | 0x1);
    mOLED_SSD1306_cmd(SSD1306_COMSCANDEC);
    mOLED_SSD1306_cmd(SSD1306_SETCOMPINS);                    // 0xDA
    mOLED_SSD1306_cmd(0x02);
    mOLED_SSD1306_cmd(SSD1306_SETCONTRAST);                   // 0x81
    mOLED_SSD1306_cmd(0x8F);
    mOLED_SSD1306_cmd(SSD1306_SETPRECHARGE);                  // 0xd9

    //mOLED_SSD1306_cmd(0x22);    //SSD1306_EXTERNALVCC 0x1
    mOLED_SSD1306_cmd(0xF1);    //SSD1306_SWITCHCAPVCC 0x2


    mOLED_SSD1306_cmd(SSD1306_SETVCOMDETECT);                 // 0xDB
    mOLED_SSD1306_cmd(0x40);
    mOLED_SSD1306_cmd(SSD1306_DISPLAYALLON_RESUME);           // 0xA4
    mOLED_SSD1306_cmd(SSD1306_NORMALDISPLAY);                 // 0xA6

    mOLED_SSD1306_cmd(SSD1306_DISPLAYON);//--turn on oled panel

    mOLED_Pen_Colour = 1;
}
Esempio n. 10
0
void Sys_Init()
{
	NVIC_Config();
	Usart1_Config();
	DMA_Config();
	SPI1_Init();
	Nrf24l01_Init(3,40);
	LED_Config();
	LED_OFF();
//	I2C_MPU_Init();
//	MPU6050_Init();
}
Esempio n. 11
0
void RtcRecoverMcuStatus( void )
{
    //Disable IRQ while the MCU is not running on HSE
    __disable_irq( );

    SystemClockConfig_STOP();
		TimerHwInit();
    SPI1_Init();
    SX1276IoInit();
		SX1276Q1CtrlInit();
    __enable_irq( );
}
void HAL_MspSleepInit(void)
{
  /* NOTE : This function is generated automatically by STM32CubeMX and eventually
            modified by the user
   */

	SystemClock_Config();

	SPI1_Init();

	SX1276IoInit( );

	TimerHwInit( );
}
Esempio n. 13
0
void Touch_Init(void)
{
  	GPIO_InitTypeDef GPIO_InitStructure;

  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//pen_int:PD6   
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  	GPIO_Init(GPIOD, &GPIO_InitStructure);		   
		SPI1_Init();
		CSPin_init();
	//GUI_TOUCH_Calibrate(0,0,319,1855,90);//X轴校准
	//GUI_TOUCH_Calibrate(1,0,239,80,1895);//Y轴校准
}
Esempio n. 14
0
//初始化SPI FLASH的IO口
void SPI_Flash_Init(void)
{

	GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOA, ENABLE );

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;  //SPI CS
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //复用推挽输出
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);
 	GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
	SPI1_Init();		   //初始化SPI
	SPI1_SetSpeed(SPI_BaudRatePrescaler_4);	//设置为18M时钟,高速模式
	SPI_FLASH_TYPE=SPI_Flash_ReadID();//读取FLASH ID.
}  
Esempio n. 15
0
void VsInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;  

#if (QXW_PRODUCT_ID==116)
	SPI1_Init();
#endif 

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);

	//VS1003 CS,XDCS
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	VS_CS_SET(1);	

	//VS1003 RST   
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;    
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	//vs1003 VS_DREQ
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;	 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	//使能外部中断4,并配置优先级
	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = EXTI4_IRQn_Priority;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);  

	//连接IO口到中断线
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4); //选择 GPIOE_2管脚用作外部中断线路

	//配置中断线4为上升沿触发 
	EXTI_InitStructure.EXTI_Line = EXTI_Line4;             //外部中断线 ,使用第4根
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    //中断模式
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//中断触发方式,上升沿
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;              //打开中断
	EXTI_Init(&EXTI_InitStructure);    //调用库函数给寄存器复制
}
Esempio n. 16
0
void SPI_MAX7456_init(void) {
  u8 osdbl_r = 0;
  u8 osdbl_w = 0;
  u32 x = 0;

  MAX7456_CS_HIGH();

  SPI1_Init();

  SPI_MAX7456_setMode((int)eeprom_buffer.params.video_mode);

  MAX7456_CS_LOW();

  //read black level register
  SPI1_TransferByte(MAX7456_OSDBL_reg_read);      //black level read register
  osdbl_r = SPI1_TransferByte(0xff);
  MAX7456_CS_HIGH();
  Delay_us(100);

  MAX7456_CS_LOW();
  Delay_us(100);
  SPI1_TransferByte(MAX7456_VM0_reg);
  SPI1_TransferByte(MAX7456_RESET | max7456_videoMode | 0x00010000);
  MAX7456_CS_HIGH();

  //set black level
  osdbl_w = (osdbl_r & 0xef);       //Set bit 4 to zero 11101111
  SPI1_TransferByte(MAX7456_OSDBL_reg);       //black level write register
  SPI1_TransferByte(osdbl_w);

  // set all rows to same charactor white level, 90%
  for (x = 0; x < MAX7456_screen_rows; x++)
  {
    SPI1_TransferByte(x + 0x10);
    SPI1_TransferByte(MAX7456_WHITE_level_120);
  }
  // define sync (auto,int,ext) and
  // making sure the Max7456 is enabled
  SPI_MAX7456_control(1);
  SPI_MAX7456_clear();



}
//初始化SPI FLASH的IO口
void W25QXX_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOG,ENABLE); //使能PB PG时钟
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //PB14
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PG7
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    GPIO_SetBits(GPIOG,GPIO_Pin_7); //PG7 拉高,防止NRF干扰
    W25QXX_CS = 1; //SPI FLASH不选中
    SPI1_Init();
    
    SPI1_SetSpeed(SPI_SPEED_2); //设置为42M时钟,高速模式
    W25QXX_TYPE = W25QXX_ReadID(); //读取FLASH ID
}
Esempio n. 18
0
void NRF24L01_Init(void)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	SPI_InitTypeDef  SPI_InitStructure; 
	NRF24L01_PDEBUG("NRF24L01_Init...\r\n");
 	RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOA, ENABLE );	

	//CE CS配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure); 
	GPIO_SetBits(GPIOA,GPIO_Pin_4);
	//IRQ
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU  ;   //上拉输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_4);
	SPI1_Init();    		//初始化SPI
		
	SPI_Cmd(SPI1, DISABLE); // 
	
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		//设置SPI工作模式:设置为主SPI
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;		//设置SPI的数据大小:SPI发送接收8位帧结构
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;		//选择了串行时钟的稳态:时钟悬空低电平
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;	//数据捕获于第一个时钟沿
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;		//NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;		//定义波特率预分频的值:波特率预分频值为256
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	//指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
	SPI_InitStructure.SPI_CRCPolynomial = 7;	//CRC值计算的多项式
	SPI_Init(SPI1, &SPI_InitStructure);  //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器

	SPI_Cmd(SPI1, ENABLE); //使能SPI外设
	
	NRF24L01_CE=0; 	//使能24L01
	NRF24L01_CSN=1;	//SPI片选取消	 	
}
Esempio n. 19
0
int simple_server(eAdrGD* fADRGD,uint8_t* fSostEth,uint8_t* nBlock, uint8_t* fIPAddr,uint8_t* fMACAddr,uint8_t* fPORTNUMBER)
	{
	SPI1_Init();

//	Del_1ms(100);
	/*initialize enc28j60*/
	pADRGD=fADRGD;
	EthSost=fSostEth;
	EthBlock=nBlock;
	PORTNUMBER=fPORTNUMBER;
	MACAddr=fMACAddr;
	IPAddr=fIPAddr;
	enc28j60Init(MACAddr);
	init_ip_arp_udp_tcp(MACAddr,IPAddr,*PORTNUMBER);
    //ָʾµÆ״̬:0x476 is PHLCON LEDA(ÂÌ)=links status, LEDB(ºì)=receive/transmit
    enc28j60PhyWrite(PHLCON,0x7a4);
	enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
//	Del_1ms(20);
	Sockets[0].IP_PHASE=0;
	//init the ethernet/ip layer:
//        return (0);
	}
/**
  * @brief  Initializes the Global MSP.
  * @param  None
  * @retval None
  */
void HAL_MspInit(void)
{
  /* NOTE : This function is generated automatically by STM32CubeMX and eventually
            modified by the user
   */

	SystemClock_Config();

	//initiate LEDs
	HalLedInit();

	//initiate KEY
	//KEY_Init();
	HalKeyInit( );

	//initiate OLED
	OLED_Init();

	//initiate SPI (for sx1276/1279)
	SPI1_Init();

	SX1276IoInit( );

	//sx1279 active crystal initiate and power on
  SX1276Q1CtrlInit();

	//initiate 3-wire UART
  UART_Init();

#if defined( LOW_POWER_MODE_ENABLE )
  TimerSetLowPowerEnable( true );
  //initiate RTC and stop mode
  RtcInit( );
#else
  TimerSetLowPowerEnable( false );
  TimerHwInit( );
#endif
}
Esempio n. 21
0
//初始化SPI FLASH的IO口
void W25QXX_Init(void)
{ 
  GPIO_InitTypeDef  GPIO_InitStructure;
 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);//使能GPIOG时钟

	  //GPIOB14
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//PB14
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
  GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//PG7
  GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化
 
	GPIO_SetBits(GPIOG,GPIO_Pin_7);//PG7输出1,防止NRF干扰SPI FLASH的通信 
	W25QXX_CS=1;			//SPI FLASH不选中
	SPI1_Init();		   			//初始化SPI
	SPI1_SetSpeed(SPI_BaudRatePrescaler_2);		//设置为42M时钟,高速模式 
	W25QXX_TYPE=W25QXX_ReadID();	//读取FLASH ID.
}  
Esempio n. 22
0
void Peripherals_Init(void)
{
#ifdef NVIC_AUTOINIT
  NVIC_Init();
#endif /* NVIC_AUTOINIT */
#ifdef SIM_AUTOINIT
  SIM_Init();
#endif /* SIM_AUTOINIT */

#ifdef MCM_AUTOINIT
  MCM_Init();
#endif /* MCM_AUTOINIT */
#ifdef PMC_AUTOINIT
  PMC_Init();
#endif /* PMC_AUTOINIT */
#ifdef PORTA_AUTOINIT
  PORTA_Init();
#endif /* PORTA_AUTOINIT */
#ifdef PORTB_AUTOINIT
  PORTB_Init();
#endif /* PORTB_AUTOINIT */
#ifdef PORTC_AUTOINIT
  PORTC_Init();
#endif /* PORTC_AUTOINIT */
#ifdef PORTD_AUTOINIT
  PORTD_Init();
#endif /* PORTD_AUTOINIT */
#ifdef PORTE_AUTOINIT
  PORTE_Init();
#endif /* PORTE_AUTOINIT */

#ifdef ADC0_AUTOINIT
  ADC0_Init();
#endif /* ADC0_AUTOINIT */
#ifdef ADC1_AUTOINIT
  ADC1_Init();
#endif /* ADC1_AUTOINIT */
#ifdef AIPS0_AUTOINIT
  AIPS0_Init();
#endif /* AIPS0_AUTOINIT */
#ifdef AIPS1_AUTOINIT
  AIPS1_Init();
#endif /* AIPS1_AUTOINIT */
#ifdef AXBS_AUTOINIT
  AXBS_Init();
#endif /* AXBS_AUTOINIT */
#ifdef CAN0_AUTOINIT
  CAN0_Init();
#endif /* CAN0_AUTOINIT */
#ifdef CMP0_AUTOINIT
  CMP0_Init();
#endif /* CMP0_AUTOINIT */
#ifdef CMP1_AUTOINIT
  CMP1_Init();
#endif /* CMP1_AUTOINIT */
#ifdef CMP2_AUTOINIT
  CMP2_Init();
#endif /* CMP2_AUTOINIT */
#ifdef CMT_AUTOINIT
  CMT_Init();
#endif /* CMT_AUTOINIT */
#ifdef CRC_AUTOINIT
  CRC_Init();
#endif /* CRC_AUTOINIT */
#ifdef DAC0_AUTOINIT
  DAC0_Init();
#endif /* DAC0_AUTOINIT */
#ifdef DMAMUX_AUTOINIT
  DMAMUX_Init();
#endif /* DMAMUX_AUTOINIT */
#ifdef DMA_AUTOINIT
  DMA_Init();
#endif /* DMA_AUTOINIT */
#ifdef ENET_AUTOINIT
  ENET_Init();
#endif /* ENET_AUTOINIT */
#ifdef EWM_AUTOINIT
  EWM_Init();
#endif /* EWM_AUTOINIT */
#ifdef FB_AUTOINIT
  FB_Init();
#endif /* FB_AUTOINIT */
#ifdef FMC_AUTOINIT
  FMC_Init();
#endif /* FMC_AUTOINIT */
#ifdef FTFE_AUTOINIT
  FTFE_Init();
#endif /* FTFE_AUTOINIT */
#ifdef FTM0_AUTOINIT
  FTM0_Init();
#endif /* FTM0_AUTOINIT */
#ifdef FTM1_AUTOINIT
  FTM1_Init();
#endif /* FTM1_AUTOINIT */
#ifdef FTM2_AUTOINIT
  FTM2_Init();
#endif /* FTM2_AUTOINIT */
#ifdef FTM3_AUTOINIT
  FTM3_Init();
#endif /* FTM3_AUTOINIT */
#ifdef I2C0_AUTOINIT
  I2C0_Init();
#endif /* I2C0_AUTOINIT */
#ifdef I2C1_AUTOINIT
  I2C1_Init();
#endif /* I2C1_AUTOINIT */
#ifdef I2C2_AUTOINIT
  I2C2_Init();
#endif /* I2C2_AUTOINIT */
#ifdef I2S0_AUTOINIT
  I2S0_Init();
#endif /* I2S0_AUTOINIT */
#ifdef LLWU_AUTOINIT
  LLWU_Init();
#endif /* LLWU_AUTOINIT */
#ifdef LPTMR0_AUTOINIT
  LPTMR0_Init();
#endif /* LPTMR0_AUTOINIT */
#ifdef MPU_AUTOINIT
  MPU_Init();
#endif /* MPU_AUTOINIT */
#ifdef PDB0_AUTOINIT
  PDB0_Init();
#endif /* PDB0_AUTOINIT */
#ifdef PIT_AUTOINIT
  PIT_Init();
#endif /* PIT_AUTOINIT */
#ifdef PTA_AUTOINIT
  PTA_Init();
#endif /* PTA_AUTOINIT */
#ifdef PTB_AUTOINIT
  PTB_Init();
#endif /* PTB_AUTOINIT */
#ifdef PTC_AUTOINIT
  PTC_Init();
#endif /* PTC_AUTOINIT */
#ifdef PTD_AUTOINIT
  PTD_Init();
#endif /* PTD_AUTOINIT */
#ifdef PTE_AUTOINIT
  PTE_Init();
#endif /* PTE_AUTOINIT */
#ifdef RCM_AUTOINIT
  RCM_Init();
#endif /* RCM_AUTOINIT */
#ifdef RNG_AUTOINIT
  RNG_Init();
#endif /* RNG_AUTOINIT */
#ifdef RTC_AUTOINIT
  RTC_Init();
#endif /* RTC_AUTOINIT */
#ifdef SDHC_AUTOINIT
  SDHC_Init();
#endif /* SDHC_AUTOINIT */
#ifdef SMC_AUTOINIT
  SMC_Init();
#endif /* SMC_AUTOINIT */
#ifdef SPI0_AUTOINIT
  SPI0_Init();
#endif /* SPI0_AUTOINIT */
#ifdef SPI1_AUTOINIT
  SPI1_Init();
#endif /* SPI1_AUTOINIT */
#ifdef SPI2_AUTOINIT
  SPI2_Init();
#endif /* SPI2_AUTOINIT */
#ifdef SystemControl_AUTOINIT
  SystemControl_Init();
#endif /* SystemControl_AUTOINIT */
#ifdef SysTick_AUTOINIT
  SysTick_Init();
#endif /* SysTick_AUTOINIT */
#ifdef UART0_AUTOINIT
  UART0_Init();
#endif /* UART0_AUTOINIT */
#ifdef UART1_AUTOINIT
  UART1_Init();
#endif /* UART1_AUTOINIT */
#ifdef UART2_AUTOINIT
  UART2_Init();
#endif /* UART2_AUTOINIT */
#ifdef UART3_AUTOINIT
  UART3_Init();
#endif /* UART3_AUTOINIT */
#ifdef UART4_AUTOINIT
  UART4_Init();
#endif /* UART4_AUTOINIT */
#ifdef UART5_AUTOINIT
  UART5_Init();
#endif /* UART5_AUTOINIT */
#ifdef USB0_AUTOINIT
  USB0_Init();
#endif /* USB0_AUTOINIT */
#ifdef USBDCD_AUTOINIT
  USBDCD_Init();
#endif /* USBDCD_AUTOINIT */
#ifdef VREF_AUTOINIT
  VREF_Init();
#endif /* VREF_AUTOINIT */
#ifdef WDOG_AUTOINIT
  WDOG_Init();
#endif /* WDOG_AUTOINIT */
}
Esempio n. 23
0
void main() {
    char error,error2;
    uart1_init(9600);
    SPI1_Init();
    buf[0]='E';
    buf[1]='n';
    buf[2]='g';
    buf[3]='S';
    buf[4]='h';
    buf[5]='a';
    buf[6]='d';
    buf[7]='y';
    

    
    trisd=255;
  
     error = MMC_init();
    if(!error){
    uart1_write_text("connected");
    }
    else
    uart1_write_text("not connected");

    
    
    while(1)
    {
    if(rd0_bit==0)
    {
    buf[0]='E';
    buf[1]='n';
    buf[2]='g';
    buf[3]='S';
    buf[4]='h';
    buf[5]='a';
    buf[6]='d';
    buf[7]='y';
     error2 = Mmc_write_Sector(1, buf);
     if(!error2)
     {
     uart1_write_text("write success");
     }
     else
     uart1_write_text("write not success");
     while(rd0_bit==0){}
    }
    
    
        if(rd1_bit==0)
    {
     error2 = Mmc_read_Sector(1, read);
     uart1_write_text(read);
     while(rd1_bit==0){}
    }
      if(rd2_bit==0)
    {
    buf[0]='E';
    buf[1]='n';
    buf[2]='g';
    buf[3]='i';
    buf[4]='s';
    buf[5]='m';
    buf[6]='a';
    buf[7]='i';
    buf[7]='l';
     error2 = Mmc_write_Sector(1, buf);
     if(!error2)
     {
     uart1_write_text("write success");
     }
     else
     uart1_write_text("write not success");
     while(rd2_bit==0){}
    }

    
    
    
    }
    



}
Esempio n. 24
0
/******************************************
*函数名称:SPI_init
*函数功能:spi1, spi2 initial
*入口参数:无	
*出口参数:无		   
*返 回 值:无
*全局变量:无
*调用函数:无
******************************************/
void SPI_init(void)
{
    SPI1_Init();
    SPI2_Init();
}
Esempio n. 25
0
//******************************************************************
//函数名:  LCD_Init
//作者:    xiao冯@全动电子
//日期:    2013-02-22
//功能:    LCD初始化
//输入参数:无
//返回值:  无
//修改记录:无
//******************************************************************
void lcd_init(void)
{  
#if USE_HARDWARE_SPI //使用硬件SPI
	SPI1_Init();
#else	
	LCD_GPIOInit();//使用模拟SPI
#endif  										 

 	LCD_RESET(); //液晶屏复位

	//************* Start Initial Sequence **********//		
	//开始初始化液晶屏
	LCD_WR_REG(0x11);//Sleep exit 
	delay_ms (120);		
	//ST7735R Frame Rate
	LCD_WR_REG(0xB1); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 

	LCD_WR_REG(0xB2); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 

	LCD_WR_REG(0xB3); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 
	
	LCD_WR_REG(0xB4); //Column inversion 
	LCD_WR_DATA(0x07); 
	
	//ST7735R Power Sequence
	LCD_WR_REG(0xC0); 
	LCD_WR_DATA(0xA2); 
	LCD_WR_DATA(0x02); 
	LCD_WR_DATA(0x84); 
	LCD_WR_REG(0xC1); 
	LCD_WR_DATA(0xC5); 

	LCD_WR_REG(0xC2); 
	LCD_WR_DATA(0x0A); 
	LCD_WR_DATA(0x00); 

	LCD_WR_REG(0xC3); 
	LCD_WR_DATA(0x8A); 
	LCD_WR_DATA(0x2A); 
	LCD_WR_REG(0xC4); 
	LCD_WR_DATA(0x8A); 
	LCD_WR_DATA(0xEE); 
	
	LCD_WR_REG(0xC5); //VCOM 
	LCD_WR_DATA(0x0E); 
	
	LCD_WR_REG(0x36); //MX, MY, RGB mode 				 
	LCD_WR_DATA(0xC8); 
	
	//ST7735R Gamma Sequence
	LCD_WR_REG(0xe0); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x1a); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x18); 
	LCD_WR_DATA(0x2f); 
	LCD_WR_DATA(0x28); 
	LCD_WR_DATA(0x20); 
	LCD_WR_DATA(0x22); 
	LCD_WR_DATA(0x1f); 
	LCD_WR_DATA(0x1b); 
	LCD_WR_DATA(0x23); 
	LCD_WR_DATA(0x37); 
	LCD_WR_DATA(0x00); 	
	LCD_WR_DATA(0x07); 
	LCD_WR_DATA(0x02); 
	LCD_WR_DATA(0x10); 

	LCD_WR_REG(0xe1); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x1b); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x17); 
	LCD_WR_DATA(0x33); 
	LCD_WR_DATA(0x2c); 
	LCD_WR_DATA(0x29); 
	LCD_WR_DATA(0x2e); 
	LCD_WR_DATA(0x30); 
	LCD_WR_DATA(0x30); 
	LCD_WR_DATA(0x39); 
	LCD_WR_DATA(0x3f); 
	LCD_WR_DATA(0x00); 
	LCD_WR_DATA(0x07); 
	LCD_WR_DATA(0x03); 
	LCD_WR_DATA(0x10);  
	
	LCD_WR_REG(0x2a);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x7f);

	LCD_WR_REG(0x2b);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x9f);
	
	LCD_WR_REG(0xF0); //Enable test command  
	LCD_WR_DATA(0x01); 
	LCD_WR_REG(0xF6); //Disable ram power save mode 
	LCD_WR_DATA(0x00); 
	
	LCD_WR_REG(0x3A); //65k mode 
	LCD_WR_DATA(0x05); 	
	LCD_WR_REG(0x29);//Display on	

	LCD_SetParam();//设置LCD参数	 
	LCD_Clear(BLACK);
	LCD_LED_CLR;//点亮背光	 
}
Esempio n. 26
0
int main(void)
{	
	

	u8 readstatus,firstread=0;
	u8 humi_set_temp=humi_set;
	u8 temp_set_temp=temp_set;
	//main1();
	/* USART1 config 115200 8-N-1 */
//	USART1_Config();
//	
//	printf("\r\n this is a yhx firmware \r\n");	
		/* 配置SysTick 为1us中断一次 */

	SysTick_Init();
	
//	TIM3_Int_Init(100,7199);
	
	DelayInit();
//	delay_init(72);
	
	/*按键输入初始化*/

	GPIO_INPUT_INIT();
	
	/*输出初始化*/
	IO_OUTPUT_Config();
	
	/* I2C 外设初(AT24C02)始化 */
	I2C_EE_Init();	
  
 /*当按下-键时,程序会执行默认的IP192.168.3.248,不会从eeprom读取数据*/
 if( Key_Scan(KEYPORT,KEY_SUB,1) != KEY_ON  )
		I2C_EE_Restore();
 if(EE_FLAG!=i2c_eeprom_data[0])
	{		
		//初始温湿度设定及裕量
		i2c_eeprom_data[0]=EE_FLAG;
		i2c_eeprom_data[1]=temp_set;
		i2c_eeprom_data[2]=humi_set;

		i2c_eeprom_data[3]=temp_distance;
		i2c_eeprom_data[4]=humi_distance;		

		//初始化net
		//ip
		i2c_eeprom_data[8]=ipaddress[0];
		i2c_eeprom_data[9]=ipaddress[1];
		i2c_eeprom_data[10]=ipaddress[2];
		i2c_eeprom_data[11]=ipaddress[3];
		//mask
		i2c_eeprom_data[12]=maskaddress[0];
		i2c_eeprom_data[13]=maskaddress[1];
		i2c_eeprom_data[14]=maskaddress[2];
		i2c_eeprom_data[15]=maskaddress[3];
		//gate
		i2c_eeprom_data[16]=gateaddress[0];
		i2c_eeprom_data[17]=gateaddress[1];
		i2c_eeprom_data[18]=gateaddress[2];
		i2c_eeprom_data[19]=gateaddress[3];
		//mac
		i2c_eeprom_data[24]=mymac[0];
		i2c_eeprom_data[25]=mymac[1];
		i2c_eeprom_data[26]=mymac[2];
		i2c_eeprom_data[27]=mymac[3];
		i2c_eeprom_data[28]=mymac[4];
		i2c_eeprom_data[29]=mymac[5];
		
		I2C_EE_WaitOperationIsCompleted();
		I2C_EE_WriteBuffer(i2c_eeprom_data, 0, 8);	
		//网络
		I2C_EE_WaitOperationIsCompleted();
		I2C_EE_WriteBuffer(i2c_eeprom_data+8, 8, 8);	
		I2C_EE_WaitOperationIsCompleted();
		I2C_EE_WriteBuffer(i2c_eeprom_data+16, 16, 8);	
		I2C_EE_WaitOperationIsCompleted();
		I2C_EE_WriteBuffer(i2c_eeprom_data+24, 24, 8);		
	}
  
	//中断开启,配置net管脚和传感器data管脚中断
//	NVIC_Configuration();
	
	/*SHT10-模拟I2C读取温湿度,初始化*/
//	Sht_GPIO_Config();	
//	s_connectionreset();
//	sht10_read();
	/*初始化DTT11的引脚*/
	DHT11_GPIO_Config();
	
	SPI1_Init();    
	//InitNet();		/* 初始化网络设备以及UIP协议栈,配置IP地址 */	
	
//数码管初始化	
  hc595_init();

/* 配置SysTick 为10ms中断一次 */
//	SysTick_Init();
	TimingInput=0;//输入节拍20ms
	IWDG_Init(4,625);    //与分频数为64,重载值为625,溢出时间为1s	   
	while(1)
	{
		
		
		/*温湿度测量,massureflag请求测量标志*/
		if(massureflag)
		{	
			//数据伪造1213,温度控制用的是真实的,显示和上传的数据用的是渐变的伪造数据
			if( (readstatus=Read_DHT11(&DHT11_Data))==SUCCESS)
			{
				
				if(firstread==0) 
				{
					temp_val.f=DHT11_Data.temp_int;
					firstread=1;
				}
				else
				{
					if(DHT11_Data.temp_int>(int)temp_val.f)
						temp_val.f=temp_val.f+0.5;
					else if(DHT11_Data.temp_int<(int)temp_val.f)
						temp_val.f=temp_val.f-0.5;
				}
				
				humi_val.f=DHT11_Data.humi_int;
				sensor_error=0;
				massureflag=0;
				time18ms_out=0;
				
				if(set_wait>1000)//初始化已过
					if((temp_val.f>-10)&&(temp_val.f<100)&&(humi_val.f>=0/*)&&(humi_val.f<100*/)&&poweron)//传感器没坏才输出
						automation();
				
		
			}else if(readstatus!=NOTREADY)
			{
				sensor_error++;				
			}
		}
		if(sensor_error>5)//5次都没有采到温湿度
		{
			temp_val.f=humi_val.f=-100;
		}
				
		
		//printf("温度:%.1f\t湿度%.1f\r\n",temp_val.f,humi_val.f);
		
		//根据检测到的温湿度控制led和继电器,刚开机3s的时候尚未检测到温湿度,此时最好不要有输出,在while之前添加sht10_read()吧,进来之前就读一次数据
		
		
		if((0==poweron)||((time_s_remainder==0)&&(active!='a')))
		{
			WARM(0);
			WARM_LED(0);
			COLD(0);
			COLD_LED(0);
			WET(0);
			WET_LED(0);
			ALWAYS_WET_LED(0);
			BEEP_SCREAM(0);
			work_state=0;
		}

			
		if(network_state==0)
		{
			//下面的代码是按键检测,用来设定温湿度设定值的
			if(TimingInput)//输入检测20ms一次
			{
				TimingInput=0;
				key_input_last=key_input;
				GPIO_INPUT_SCAN();		
				if(key_input==key_input_last)//检测是否一直按着同一个按键
					press_same_key_flag=1;//一直按着同一个按键
				else press_same_key_flag=0;
				
				//蜂鸣器响应
				if((key_input)&&(beeptime<300))
				{			
						//蜂鸣器响
						BEEP_SCREAM(1);
					
				}else
				{
					if(key_input==0)
						beeptime=0;
					//蜂鸣器关闭
					BEEP_SCREAM(0);
				}			
				
				//是否进入设定状态,闪烁。
				if(0==showdigital)//之前就是闪烁状态,temp_set_temp数据要保留
				{
					temp_set_temp=temp_set;
					humi_set_temp=humi_set;
				}
				
				if((0==press_same_key_flag)&&(0!=key_input))//有按键按下但不是同一个按键
				{
					
					//输入处理
					switch (key_input)
					{
					case 1://set
						if(set_wait<=1000)
							set_start_flag=1;
						showdigital++;//第showdigital位闪烁
						if(showdigital>4)
							showdigital=1;
						break;
					case 2://save
						showdigital=0;
						set_start_flag=0;//初始化完成标志
						set_wait=1001;//设置等待10s结束
						//将数据保存到eeprom,温湿度设定值
						temp_set=temp_set_temp;
						humi_set=humi_set_temp;
						i2c_eeprom_data[1]=temp_set;
						i2c_eeprom_data[2]=humi_set;
						
						//写入eeprom
						i2c_eeprom_data[0]=EE_FLAG;
						I2C_EE_WaitOperationIsCompleted();
						I2C_EE_WriteBuffer(i2c_eeprom_data, 0, 5);
						break;
					case 4://+
						//第showdigital位+
						switch(showdigital)
						{
						case 1:
							temp_set_temp=(temp_set_temp/10+1)%10*10+temp_set_temp%10;
							break;
						case 2:
							temp_set_temp=temp_set_temp/10*10+(temp_set_temp%10+1)%10;
							break;
						case 3:
							humi_set_temp=(humi_set_temp/10+1)%10*10+humi_set_temp%10;
							break;
						case 4:
							humi_set_temp=humi_set_temp/10*10+(humi_set_temp%10+1)%10;
							break;
						default:;
						}
						break;
					case 8://-
						//第showdigital位+
						switch(showdigital)
						{
						case 1:
							temp_set_temp=(temp_set_temp<10)?90+temp_set_temp:(temp_set_temp/10-1)%10*10+temp_set_temp%10;
							break;
						case 2:
							temp_set_temp=(temp_set_temp%10==0)?temp_set_temp+9:temp_set_temp-1;
							break;
						case 3:
							humi_set_temp=(humi_set_temp<10)?90+humi_set_temp:(humi_set_temp/10-1)%10*10+humi_set_temp%10;
							break;
						case 4:
							humi_set_temp=(humi_set_temp%10==0)?humi_set_temp+9:humi_set_temp-1;
							break;
						default:;
						}
						break;
					default:;
					}
				}
			}
		}else
		{//本地状态显示
			humi_set_temp=humi_set;
			temp_set_temp=temp_set;
			key_input=0;
			showdigital=0;
			BEEP_SCREAM(0);
			beeptime=0;
		}
		
		//试用期写入
		if((active!='a')&&(remainder_write>300))
		{
			remainder_write=0;
			//写入eeprom
			//if(time_s_remainder>0)
			{
				i2c_eeprom_data[32]=(u8)time_s_remainder;
				i2c_eeprom_data[33]=(u8)(time_s_remainder>>8);
				i2c_eeprom_data[34]=(u8)(time_s_remainder>>16);
				i2c_eeprom_data[35]=(u8)(time_s_remainder>>24);
				I2C_EE_WaitOperationIsCompleted();
				I2C_EE_WriteBuffer(i2c_eeprom_data+32, 32, 4);
			}
		}
//sht10_j:		
		//数码管输出
		
		digitaldata[0]=digitaldata[1]=digitaldata[2]=digitaldata[3]=digitaldata[4]=digitaldata[5]=12;//12
		if((active=='a')||(time_s_remainder>0))//激活态20150421
		{
			//温度
			if((temp_val.f<0)&&(temp_val.f>-10))//负值
			{
				digitaldata[0]=10;//-
				digitaldata[1]=(u8)(-temp_val.f);
				digitaldata[2]=(u8)(-temp_val.f*10)%10;
			}else if((temp_val.f>=0)&&(temp_val.f<100))//正数
			{
				digitaldata[0]=((u8)temp_val.f)/10;
				digitaldata[1]=((u8)temp_val.f)%10;//.
				digitaldata[2]=(u8)(((u16)(temp_val.f*10))%10);
			}
			//湿度
			if(humi_val.f>=0)//((humi_val.f>=0)&&(humi_val.f<100))
			{
				if(humi_val.f>=100) 
				{
					temp_f=humi_val.f;//20150712 add temp xzk
					humi_val.f=99.9;
				}
				
				digitaldata[3]=((u8)humi_val.f)/10;
				digitaldata[4]=((u8)humi_val.f)%10;//.
				digitaldata[5]=(u8)(((u16)(humi_val.f*10))%10);
				if(humi_val.f>=99.9)
					humi_val.f=temp_f;
			}
		}else	//试用期已经到了,显示off 20150421
		{
			digitaldata[0]=0;
			digitaldata[3]=0;
			BEEP_SCREAM(1);
		}
		//设定温度值
		digitaldata[6]=temp_set_temp/10;
		digitaldata[7]=temp_set_temp%10;
		//湿度设定
		digitaldata[8]=humi_set_temp/10;
		digitaldata[9]=humi_set_temp%10;
		
		if((blickflag)&&(network_state==0))//可以闪烁			
			switch(showdigital)
			{//showdigital=0//低位到高位是否显示6 7 8 9,0都显示,1:9不显示;2:8不显示;3:7不显示;4:6不显示
				case 1:
					digitaldata[6]=11;
					break;
				case 2:
					digitaldata[7]=11;
					break;
				case 3:
					digitaldata[8]=11;
					break;
				case 4:
					digitaldata[9]=11;
					break;
				default:;
			}
		hc595_display(digitaldata);		
			
			
		//就地状态或网络状态PC3
		if( Key_Scan(GPIOC,GPIO_Pin_3,0) == KEY_ON  )
		{
			/*按键有效*/
			network_state=1;
			
		}else//就地
		{
			network_state=0;
			//poweron=1; //0716
			//启动开关PA12检测 20150719
			if( Key_Scan(GPIOA,GPIO_Pin_12,1) == KEY_ON  )
			{
				poweron=1;
			}else
			{
				poweron=0;
			}
		}
			
			
		/* 下面是网络检测的程序,处理uip事件,必须插入到用户程序的循环体中 */
		UipPro();			  //中断触发读取网络接收缓存
		eth_poll();           //定时查询TCP及UDP连接收发状态	ARP表更新, 并响应		
		
		IWDG_Feed();//喂狗
		
		if(request_initnet>250)//等待2s初始化网络
		{
			InitNet();
			request_initnet=0;
		}
	}    
Esempio n. 27
0
//初始化ENC28J60
//macaddr:MAC地址
//返回值:0,初始化成功;
//       1,初始化失败;
u8 ENC28J60_Init(void)
{
	u8 version;
	u16 retry=0;
	u32 temp;
	
	GPIO_InitTypeDef GPIO_InitStructure;
	SPI_InitTypeDef  SPI_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);	 //使能PA,C端口时钟
    	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   	//推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
 	GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);	//PA2,3,4置高
 	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				//PC4 推挽 	  
 	GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_SetBits(GPIOC,GPIO_Pin_4);							//PC4上拉
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_1;   			//中断引脚PA1上拉输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	//PA1外部中断,中断线1
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line1;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	EXTI_ClearITPendingBit(EXTI_Line1); //清除中断线1挂起标志位
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;			//外部中断线1
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;			//子优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
						  
	SPI1_Init();    		//初始化SPI	 
	SPI_Cmd(SPI1, DISABLE); // SPI外设不使能

	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //SPI设置为双线双向全双工
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		//SPI主机
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;	//发送接收8位帧结构
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;			//时钟悬空低
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;		//数据捕获于第1个时钟沿
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;			//NSS信号由软件控制
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;		//定义波特率预分频的值:波特率预分频值为256
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	//数据传输从MSB位开始
	SPI_InitStructure.SPI_CRCPolynomial = 7;			//CRC值计算的多项式
	SPI_Init(SPI1, &SPI_InitStructure);  				//根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
	SPI_Cmd(SPI1, ENABLE); //使能SPI外设
	
	SPI1_SetSpeed(SPI_BaudRatePrescaler_8);	//SPI1 SCK频率为36M/4=4.5Mhz
	//初始化MAC地址
	temp=*(vu32*)(0x1FFFF7E8);	//获取STM32的唯一ID的前24位作为MAC地址后三字节
	enc28j60_dev.macaddr[0]=2;
	enc28j60_dev.macaddr[1]=0;
	enc28j60_dev.macaddr[2]=0;
	enc28j60_dev.macaddr[3]=(temp>>16)&0XFF;	//低三字节用STM32的唯一ID
	enc28j60_dev.macaddr[4]=(temp>>8)&0XFFF;
	enc28j60_dev.macaddr[5]=temp&0XFF;

	ENC28J60_RST=0;			//复位ENC28J60
	delay_ms(10);	 
	ENC28J60_RST=1;			//复位结束				    
	delay_ms(10);	
	ENC28J60_Write_Op(ENC28J60_SOFT_RESET,0,ENC28J60_SOFT_RESET);	//软件复位
	while(!(ENC28J60_Read(ESTAT)&ESTAT_CLKRDY)&&retry<250)	//等待时钟稳定
	{
		retry++;
		delay_ms(1);
	}	
	if(retry>=250)return 1; //ENC28J60初始化失败
	version=ENC28J60_Get_EREVID();			//获取ENC28J60的版本号
	printf("ENC28J60 Version:%d\r\n",version);	
	
	enc28j60_dev.NextPacketPtr=RXSTART_INIT;
	//接收缓冲器由一个硬件管理的循环FIFO 缓冲器构成。
	//寄存器对ERXSTH:ERXSTL 和ERXNDH:ERXNDL 作
	//为指针,定义缓冲器的容量和其在存储器中的位置。
	//ERXST和ERXND指向的字节均包含在FIFO缓冲器内。
	//当从以太网接口接收数据字节时,这些字节被顺序写入
	//接收缓冲器。 但是当写入由ERXND 指向的存储单元
	//后,硬件会自动将接收的下一字节写入由ERXST 指向
	//的存储单元。 因此接收硬件将不会写入FIFO 以外的单
	//元。
	//设置接收起始字节	
	ENC28J60_Write(ERXSTL,RXSTART_INIT&0XFF);	//设置接收缓冲区起始地址低8位
	ENC28J60_Write(ERXSTH,RXSTART_INIT>>8);		//设置接收缓冲区起始地址高8位
	//设置接收接收字节
	ENC28J60_Write(ERXNDL,RXSTOP_INIT&0XFF);	
	ENC28J60_Write(ERXNDH,RXSTOP_INIT>>8);
	//设置发送起始字节
	ENC28J60_Write(ETXSTL,TXSTART_INIT&0XFF);
	ENC28J60_Write(ETXSTH,TXSTART_INIT>>8);
	//设置发送结束字节
	ENC28J60_Write(ETXNDL,TXSTOP_INIT&0XFF);
	ENC28J60_Write(ETXNDH,TXSTOP_INIT>>8);
	//ERXWRPTH:ERXWRPTL 寄存器定义硬件向FIFO 中
	//的哪个位置写入其接收到的字节。 指针是只读的,在成
	//功接收到一个数据包后,硬件会自动更新指针。 指针可
	//用于判断FIFO 内剩余空间的大小  8K-1500。 
	//设置接收读指针字节
	ENC28J60_Write(ERXRDPTL,RXSTART_INIT&0XFF);
	ENC28J60_Write(ERXRDPTH,RXSTART_INIT>>8);
	//接收过滤器
	//UCEN:单播过滤器使能位
	//当ANDOR = 1 时:
	//1 = 目标地址与本地MAC 地址不匹配的数据包将被丢弃
	//0 = 禁止过滤器
	//当ANDOR = 0 时:
	//1 = 目标地址与本地MAC 地址匹配的数据包会被接受
	//0 = 禁止过滤器
	//CRCEN:后过滤器CRC 校验使能位
	//1 = 所有CRC 无效的数据包都将被丢弃
	//0 = 不考虑CRC 是否有效
	//PMEN:格式匹配过滤器使能位
	//当ANDOR = 1 时:
	//1 = 数据包必须符合格式匹配条件,否则将被丢弃
	//0 = 禁止过滤器
	//当ANDOR = 0 时:
	//1 = 符合格式匹配条件的数据包将被接受
	//0 = 禁止过滤器
	ENC28J60_Write(ERXFCON,ERXFCON_UCEN|ERXFCON_CRCEN|ERXFCON_PMEN);
	ENC28J60_Write(EPMM0,0X3F);
	ENC28J60_Write(EPMM1,0X30);
	ENC28J60_Write(EPMCSL,0Xf9);
	ENC28J60_Write(EPMCSH,0Xf7);
	//bit 0 MARXEN:MAC 接收使能位
	//1 = 允许MAC 接收数据包
	//0 = 禁止数据包接收
	//bit 3 TXPAUS:暂停控制帧发送使能位
	//1 = 允许MAC 发送暂停控制帧(用于全双工模式下的流量控制)
	//0 = 禁止暂停帧发送
	//bit 2 RXPAUS:暂停控制帧接收使能位
	//1 = 当接收到暂停控制帧时,禁止发送(正常操作)
	//0 = 忽略接收到的暂停控制帧
	ENC28J60_Write(MACON1,MACON1_MARXEN|MACON1_TXPAUS|MACON1_RXPAUS);
	//将MACON2 中的MARST 位清零,使MAC 退出复位状态。
	ENC28J60_Write(MACON2,0x00);
		//bit 7-5 PADCFG2:PACDFG0:自动填充和CRC 配置位
	//111 = 用0 填充所有短帧至64 字节长,并追加一个有效的CRC
	//110 = 不自动填充短帧
	//101 = MAC 自动检测具有8100h 类型字段的VLAN 协议帧,并自动填充到64 字节长。如果不
	//是VLAN 帧,则填充至60 字节长。填充后还要追加一个有效的CRC
	//100 = 不自动填充短帧
	//011 = 用0 填充所有短帧至64 字节长,并追加一个有效的CRC
	//010 = 不自动填充短帧
	//001 = 用0 填充所有短帧至60 字节长,并追加一个有效的CRC
	//000 = 不自动填充短帧
	//bit 4 TXCRCEN:发送CRC 使能位
	//1 = 不管PADCFG如何,MAC都会在发送帧的末尾追加一个有效的CRC。 如果PADCFG规定要
	//追加有效的CRC,则必须将TXCRCEN 置1。
	//0 = MAC不会追加CRC。 检查最后4 个字节,如果不是有效的CRC 则报告给发送状态向量。
	//bit 0 FULDPX:MAC 全双工使能位
	//1 = MAC工作在全双工模式下。 PHCON1.PDPXMD 位必须置1。
	//0 = MAC工作在半双工模式下。 PHCON1.PDPXMD 位必须清零。
	ENC28J60_Write(MACON3,MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN|MACON3_FULDPX);
	// 最大帧长度 1518
	ENC28J60_Write(MAMXFLL,MAX_FRAMELEN&0XFF);
	ENC28J60_Write(MAMXFLH,MAX_FRAMELEN>>8);
	//配置背对背包间间隔寄存器MABBIPG。当使用
	//全双工模式时,大多数应用使用15h 编程该寄存
	//器,而使用半双工模式时则使用12h 进行编程。
	ENC28J60_Write(MABBIPG,0x15);
	//配置非背对背包间间隔寄存器的低字节
	//MAIPGL。 大多数应用使用12h 编程该寄存器。
	//如果使用半双工模式,应编程非背对背包间间隔
	//寄存器的高字节MAIPGH。 大多数应用使用0Ch
	//编程该寄存器。
	ENC28J60_Write(MAIPGL,0x12);
	ENC28J60_Write(MAIPGH,0x0C);
	//设置MAC地址
	ENC28J60_Write(MAADR5,enc28j60_dev.macaddr[0]);
	ENC28J60_Write(MAADR4,enc28j60_dev.macaddr[1]);
	ENC28J60_Write(MAADR3,enc28j60_dev.macaddr[2]);
	ENC28J60_Write(MAADR2,enc28j60_dev.macaddr[3]);
	ENC28J60_Write(MAADR1,enc28j60_dev.macaddr[4]);
	ENC28J60_Write(MAADR0,enc28j60_dev.macaddr[5]);
	//配置PHY为全双工  LEDB为拉电流
	ENC28J60_PHY_Write(PHCON1,PHCON1_PDPXMD);	
	//HDLDIS:PHY 半双工环回禁止位
	//当PHCON1.PDPXMD = 1 或PHCON1.PLOOPBK = 1 时:
	//此位可被忽略。
	//当PHCON1.PDPXMD = 0 且PHCON1.PLOOPBK = 0 时:
	//1 = 要发送的数据仅通过双绞线接口发出
	//0 = 要发送的数据会环回到MAC 并通过双绞线接口发出
	ENC28J60_PHY_Write(PHCON2,PHCON2_HDLDIS);
	//ECON1 寄存器
	//寄存器3-1 所示为ECON1 寄存器,它用于控制
	//ENC28J60 的主要功能。 ECON1 中包含接收使能、发
	//送请求、DMA 控制和存储区选择位。	   
	ENC28J60_Set_Bank(ECON1);
	//EIE: 以太网中断允许寄存器
	//bit 7 INTIE: 全局INT 中断允许位
	//1 = 允许中断事件驱动INT 引脚
	//0 = 禁止所有INT 引脚的活动(引脚始终被驱动为高电平)
	//bit 6 PKTIE: 接收数据包待处理中断允许位
	//1 = 允许接收数据包待处理中断
	//0 = 禁止接收数据包待处理中断
	ENC28J60_Write_Op(ENC28J60_BIT_FIELD_SET,EIE,EIE_INTIE|EIE_PKTIE|EIE_TXIE|EIE_TXERIE|EIE_RXERIE);
	// enable packet reception
	//bit 2 RXEN:接收使能位
	//1 = 通过当前过滤器的数据包将被写入接收缓冲器
	//0 = 忽略所有接收的数据包
	ENC28J60_Write_Op(ENC28J60_BIT_FIELD_SET,ECON1,ECON1_RXEN);
	printf("ENC28J60 Duplex:%s\r\n",ENC28J60_Get_Duplex()?"Full Duplex":"Half Duplex");	//获取双工方式
	return 0;
}
Esempio n. 28
0
int main(void)
{

    HAL_Init();         // 初始化节拍器
    SystemClock_Config();   // 初始化时钟
    LED_Init(LED_AMBER);        // 初始化LED
    UART7_Init();

    POWER_Init();
    //1秒一次定时器  用于刷主循环
    TIM5_Init();


    SPI1_Init();     // 初始化SPI1 用于操作传感器
    MPU6000_Init();  // 初始化MPU6000

    LSM303D_Init();



    I2C2_Init();
    RGBLED_Init();

    while (1)
    {
        if(1 == debug_message)
        {
            debug_message = 0;
            LED_Toggle(LED_AMBER);
            //秒级 调试信息
            // mpu6000信息
            DebugPrint("MPU6000 ACCEL x=%d, y=%d, z=%d\r\n",
                    MPU_report1.accel_x_raw,
                    MPU_report1.accel_y_raw,
                    MPU_report1.accel_z_raw);


            DebugPrint("MPU6000 GYRO  x=%d, y=%d, z=%d\r\n",
                    MPU_report1.gyro_x_raw,
                    MPU_report1.gyro_y_raw,
                    MPU_report1.gyro_z_raw);
            DebugPrint("cnt=%d\r\n\r\n",MPU_RD_CNT);
            MPU_RD_CNT=0;

            //  DebugPrint("LSM303D ACCEL x=%d, y=%d, z=%d\r\n",
            //              LSM303D_ACC_report1.accel_x_raw,
            //              LSM303D_ACC_report1.accel_y_raw,
            //              LSM303D_ACC_report1.accel_z_raw);

            // LSM303D MAG信息
            DebugPrint("LSM303D MAG x=%d, y=%d, z=%d\r\n",
                        LSM303D_MAG_report1.mag_x_raw,
                        LSM303D_MAG_report1.mag_y_raw,
                        LSM303D_MAG_report1.mag_z_raw);
            DebugPrint("cnt=%d\r\n\r\n",LSM303D_RD_CNT);
            LSM303D_RD_CNT=0;
        }

    }

}
Esempio n. 29
0
//初始化
void LTC2402Init(void)
{
    SPI1_Init();
	SPI1_SetSpeed(SPI_BaudRatePrescaler_128);
	GPIO_ResetBits(GPIOA, GPIO_Pin_8);//cs - low
}
Esempio n. 30
0
void main() {
    char error,error2;
    unsigned int c =0 ,i,x,y;
    SPI1_Init();
    Glcd_init();
   // glcd_image(logo);
    GLCD_fill(255);
    i=0;
 /*   for(y=0;y<64;y++){
      for(x=0;x<128;x++){
        glcd_dot(x,y,logo[x+(i*128)]);
       }
       i++;
    }*/
    i = 128*10;
    for(x=i;x<i+128;x++){
      glcd_dot(x,1,logo[x]);
      }
    
    delay_ms(1000);
    
    while(1){
    error = MMC_init();
    if(!error){
      glcd_fill(0);
      glcd_write_text("Connected!",1,1,2);
      while(!error){
          error= MMC_init();
          if(RC2_bit==0){
             for(i=0;i<512;i++){
               xyz[i] = logo[i];
             }
            error2 = Mmc_write_Sector(1, xyz);
            for(i=0;i<512;i++){
               xyz[i] = logo[512+i];
             }
            error2 = Mmc_write_Sector(2, xyz);
            if(!error2){
               glcd_fill(0);
               glcd_write_text("Write Succ!",1,1,2);
               delay_ms(2000);
               glcd_fill(0);
               glcd_write_text("Connected!",1,1,2);
            }
            else{
               glcd_fill(0);
               glcd_write_text("Error",1,1,2);
            }
          }
          if(RC1_bit==0){
            error2 = Mmc_read_Sector(1, xyz);
            for(i=0;i<512;i++){
             //  logo[i] = xyz[i];
             }
            error2 = Mmc_write_Sector(2, xyz);
            for(i=0;i<512;i++){
              // logo[512+i] = xyz[i] ;
             }
            if(!error2){
               glcd_fill(0);

               delay_ms(2000);
               glcd_fill(0);
               glcd_write_text("Connected!",1,1,2);
            }
          }
      }
    }
    else{
      glcd_fill(0);
      glcd_write_text("Not Connected!",1,1,2);
    }
    delay_ms(200);
    }
}