161
ARM Processors / stm32f4 Training
« on: December 07, 2013, 12:51:43 pm »
stm32f4 training ภาษาไทยครับ
http://www.youtube.com/v/PTHhbS3xFd4
http://www.youtube.com/v/P5dAnZPo1T8
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : V2.0.1
* Date : 06/13/2008
* Description : Main program body
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Private define ------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include <stdio.h>
//#include <math.h>
#include "platform_config.h"
#include "GLCD5110.H"
/* Private typedef -----------------------------------------------------------*/
#define SPI_FLAG_RXNE SPI_I2S_FLAG_RXNE
#define SPI_FLAG_TXE SPI_I2S_FLAG_TXE
//#define SPI_FLAG_CRCERR ((u16)0x0010)
//#define SPI_FLAG_MODF ((u16)0x0020)
#define SPI_FLAG_OVR SPI_I2S_FLAG_OVR
#define SPI_FLAG_BSY SPI_I2S_FLAG_BSY
//----------------------------------------------
#define SPI_GetFlagStatus SPI_I2S_GetFlagStatus
#define SPI_SendData SPI_I2S_SendData
#define SPI_ReceiveData SPI_I2S_ReceiveData
#define CS_H() GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET)
#define CS_L() GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET)
#define WAVEFORM 0x01
#define AENERGY 0x02
#define VAENERGY 0x05
#define MODE 0x09
#define IRQEN 0x0A
#define IRMS 0x16
#define VRMS 0x17
#define LINECYC 0x1C
#define SAGCYC 0x1E
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
static vu32 TimingDelay;
char str[20]; // Keep string convert
s32 AE = 0, W =0;
u32 VA =0, I =0, V =0;
u16 M;
u8 b;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void SPI_Configuration(void);
void GPIO_Configuration(void);
void delay(void);
//void DelayuS(vu32 nCount); // 1uS Delay
void DelayuS(vu32 nTime); // 1uS Delay
void TimingDelay_Decrement(void);
u8 SPI_Send_Byte(u8);
u8 SPI_RD_Reg(u8);
u16 SPI_RD_Reg16(u8);
u32 SPI_RD_Reg24(u8);
void SPI_WR_Reg8(u8, u8);
void SPI_WR_Reg16(u8, u16 );
/* Private functions ---------------------------------------------------------*/
/** read out parameters cal_param from BMP085 memory
\return result of communication routines
*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration **********************************************/
RCC_Configuration();
/* NVIC Configuration *******************************************************/
NVIC_Configuration();
/* I/O init */
GPIO_Configuration();
SPI_Configuration();
/* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */
SysTick_SetReload(9);
/* Enable SysTick interrupt */
SysTick_ITConfig(ENABLE);
lcdString(1,1,"Energy Meter "); // Set message on line 1
lcdUpdate(); // Display message
DelayuS(100000);
//SPI_WR_Reg16(MODE, 12);
SPI_WR_Reg8(SAGCYC, 7);
DelayuS(20000);
while(1)
{
/*AE = SPI_RD_Reg(AENERGY);
sprintf(str,"AE : %d ", AE);
lcdString(1,2,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000);
VA = SPI_RD_Reg(VAENERGY);
sprintf(str,"VA : %d ", VA);
lcdString(1,3,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000);
I = SPI_RD_Reg24(IRMS);
sprintf(str,"I : %d ", I);
lcdString(1,4,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000); */
/*V = SPI_RD_Reg(VRMS);
sprintf(str,"V : %d ", V);
lcdString(1,5,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000);
W = SPI_RD_Reg(WAVEFORM);
sprintf(str,"AE : %d ", W);
lcdString(1,6,str); // Set message on line 4
lcdUpdate(); // Display message */
b = SPI_RD_Reg(IRQEN);
sprintf(str,"IRQEN : %d ", b);
lcdString(1,3,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000);
b = SPI_RD_Reg(SAGCYC);
sprintf(str,"SAG : %d ", b);
lcdString(1,4,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000);
M = SPI_RD_Reg16(LINECYC);
sprintf(str,"LINE : %d ", M);
lcdString(1,5,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(20000);
M = SPI_RD_Reg16(MODE);
sprintf(str,"MODE : %d ", M);
lcdString(1,6,str); // Set message on line 4
lcdUpdate(); // Display message
DelayuS(200000);
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure SPI2 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// SPI ship select CS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* SPI2 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//SPI_CPOL_High=ģʽ3£¬Ê±ÖÓ¿ÕÏÐΪ¸ß //SPI_CPOL_Low=ģʽ0£¬Ê±ÖÓ¿ÕÏÐΪµÍ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//SPI_CPHA_2Edge;//SPI_CPHA_1Edge, SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Soft;//SPI_NSS_Hard
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//SPI_BaudRatePrescaler_2=18M;//SPI_BaudRatePrescaler_4=9MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//Êý¾Ý´Ó¸ßλ¿ªÊ¼·¢ËÍ
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
/*******************************************************************************
* Function Name : SPI_FLASH_SendByte
* Description : Sends a byte through the SPI interface and return the byte
* received from the SPI bus.
* Input : byte : byte to send.
* Output : None
* Return : The value of the received byte.
*******************************************************************************/
u8 SPI_Send_Byte(u8 dat)
{
/* Loop while DR register in not emplty */
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET);
/* Send byte through the SPI2 peripheral */
SPI_SendData(SPI2, dat);
/* Wait to receive a byte */
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_ReceiveData(SPI2);
}
/*******************************************************************************
* Function Name : Serial Read 24 bits Command
* Description :
*
* Input : byte : byte to send.
* Output : None
* Return : The value of the received byte.
*******************************************************************************/
u8 SPI_RD_Reg(u8 reg)
{
u8 valve;
CS_L(); // CS low, initialize SPI communication...
SPI_Send_Byte(reg); // Select register to read from..
DelayuS(5);
valve= SPI_Send_Byte(0); // ..then read registervalue
CS_H(); // CS high, terminate SPI communication
return(valve); // return register value
}
u16 SPI_RD_Reg16(u8 reg)
{
u16 valve;
u16 valve1;
CS_L(); // CS low, initialize SPI communication...
SPI_Send_Byte(reg); // Select register to read from..
DelayuS(5);
valve= SPI_Send_Byte(0); // ..then read registervalue
valve1 = valve<<8;
valve = SPI_Send_Byte(0); // ..then read registervalue
valve1 +=valve;
CS_H(); // CS high, terminate SPI communication
return(valve1); // return register value
}
u32 SPI_RD_Reg24(u8 reg)
{
u32 reg_val;
CS_L(); // CS low, initialize SPI communication...
SPI_Send_Byte(reg); // Select register to read from..
DelayuS(4);
reg_val = SPI_Send_Byte(0)<<16; // ..then read registervalue
reg_val += SPI_Send_Byte(0)<<8; // ..then read registervalue
reg_val += SPI_Send_Byte(0); // ..then read registervalue
CS_H(); // CS high, terminate SPI communication
return(reg_val); // return register value
}
void SPI_WR_Reg8(u8 reg, u8 value)
{
CS_L(); // CSN low, init SPI transaction
SPI_Send_Byte(reg |0x80);// select register
DelayuS(4);
SPI_Send_Byte(value); // ..and write value to it..
CS_H(); // CSN high again
}
void SPI_WR_Reg16(u8 reg, u16 value)
{
CS_L(); // CSN low, init SPI transaction
SPI_Send_Byte(reg |0x80);// select register
DelayuS(4);
SPI_Send_Byte(value>>8); // ..and write value to it..
DelayuS(4);
SPI_Send_Byte(value); // ..and write value to it..
CS_H(); // CSN high again
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
//NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
// 2 bits for Preemption Priority and 2 bits for Sub Priority
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/*NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); */
// Set SysTick interrupt vector Preemption Priority to 1
//NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 0);
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
/*void DelayuS(vu32 nCount)
{
while (nCount--);
} */
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nTime: specifies the delay time length, in milliseconds.
* Output : None
* Return : None
*******************************************************************************/
void DelayuS(u32 nTime)
{
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay != 0);
/* Disable SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Clear SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Clear);
}
/*******************************************************************************
* Function Name : TimingDelay_Decrement
* Description : Decrements the TimingDelay variable.
* Input : None
* Output : TimingDelay
* Return : None
*******************************************************************************/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/