Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // using timer to trigger adc and read the cpu temperature and display the temprature to serial monitor
- #include "stm32f4xx.h" // Device header
- #include "stdio.h"
- void USART2_Init(void);
- void Timer2_init(void);
- void ADC_Init(void);
- int temp;
- double voltage,degree;
- int main(void)
- {
- USART2_Init();
- Timer2_init();
- ADC_Init();
- printf("ADC Temperature\r\n");
- while(1){
- while(!(ADC1->SR)&ADC_SR_EOC){;}
- temp=ADC1->DR;
- voltage=(double)temp/4096*3.3;
- degree=(voltage-0.76)/0.0025+25;
- printf("%d,%.2f\370\r\n",temp,degree);
- }
- }
- void USART2_Init(void){
- RCC->APB1ENR|=0x20000;
- RCC->AHB1ENR|=1;
- GPIOA->AFR[0]=0x0700;
- //GPIOA->AFR[1]=0x0700;
- GPIOA->MODER|=0x0020; //set PA2 as alternate mode
- USART2->BRR = 0x008B; //9600 @16MHz
- USART2->CR1 |=0x2008; // enable tx
- //USART2->CR1 |=0x2000;
- }
- void Timer2_init(void){
- RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN;
- RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;
- TIM2->PSC=249;
- TIM2->ARR=63999;
- TIM2->CNT=0;
- TIM2->CCMR1|=0x6800;
- TIM2->CCER|=0x0010;
- TIM2->CCR2|=50-1;
- TIM2->CCR1|=1;
- }
- void ADC_Init(void){
- //RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN; //enable gpio a clock
- RCC->APB2ENR|=RCC_APB2ENR_ADC1EN; //enable adc clock
- ADC->CCR=0;
- ADC->CCR=ADC_CCR_TSVREFE;
- //ADC->CCR&=~ADC_CCR_VBATE;
- ADC1->SMPR1|=0x1C0000;
- ADC1->SQR1|=1;
- ADC1->CR2=0x13000000;
- ADC1->CR2|=1;
- }
- int USART2_write(int ch ){
- while(!(USART2->SR)){;}
- USART2->DR =(ch&0xFF);
- return ch;
- }
- struct __FILE{int handle;};
- FILE __stdout ={1};
- int fputc(int c, FILE *f){
- return USART2_write(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement