//*******************************************************************************
// MCU : STM32F030C6
// Crystal : Internal 8MHz
Code: Select all
// LCD module connections
sbit LCD_RS at GPIOB_ODR.B3;
sbit LCD_EN at GPIOB_ODR.B4;
sbit LCD_D4 at GPIOB_ODR.B5;
sbit LCD_D5 at GPIOB_ODR.B6;
sbit LCD_D6 at GPIOB_ODR.B7;
sbit LCD_D7 at GPIOB_ODR.B8;
// End LCD module connections
char byte[16],i;
char rxchar='$';
unsigned short ready,header=0,flag=0;
unsigned int check_sum_total=0,check_sum=1234;
char *Fan_RPM=" 000";
void interrupt() iv IVT_INT_USART1 ics ICS_AUTO
{
if(RXNEIE_USART1_CR1_bit == 1) // are Rx data ready ints enabled ?
{
if(RXNE_USART1_SR_bit == 1) // yes, is interupt pending ?
{
if(header==1) // If Header is OK read next n bytes
{
rxchar = UART1_Read(); //
byte[i] = rxchar;
i++;
if(i==8) // Read byte till 38 byte receive
{
for(i=1;i<8;i++)
{
check_sum_total=check_sum_total+byte[i];
}
check_sum=check_sum_total&255;
{
GPIOB_ODR.B15=~GPIOB_ODR.B15; // Just for indication
header=0;// RESET HEADER
}
}
} // End (header==1)
if(header==0) // Check the header
{
rxchar = UART1_Read(); //
if(rxchar==42) // HEADER if '*' or not
{
GPIOB_ODR.B14=~GPIOB_ODR.B14; // Just for indication
header=1; // Header OK, allow next byte to store in buffer
check_sum_total=0; // Reset Check SUM
i=0; // Reset byte count
flag=1;
}
} // END Header check
RXNE_USART1_SR_bit = 0 ; // Clear Interrupt Flag
}
} // END Interrupt Flag
} // End UART ISR
void display()
{
if(flag==1)
{
if(byte[2]==51) // if 3rd byte is '3'
{
Lcd_Out(3,10,"OK");
Lcd_Out(2,1,byte);
}
else
Lcd_Out(3,10,"NO");
flag=0;
}
}
void main()
{
GPIO_Digital_Output(&GPIOB_ODR, _GPIO_PINMASK_14); // Set PORTC9 as digital output
GPIO_Digital_Output(&GPIOB_ODR, _GPIO_PINMASK_15); // Set PORTC9 as digital output
GPIOB_ODR.B14=0;
GPIOB_ODR.B15=1;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"STM Blue Pill"); // Write text in second row
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
UART1_Init_Advanced(2400, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART1_PA9_10);
RCC_APB2ENR.USART1EN = 1 ; // Turn on USART1 module clocks
USART1_CR1bits.UE = 1; //Set USART1
USART1_CR1bits.RE = 1; //Set USART1 RX to receive incoming data
USART1_CR1bits.TE = 1; //Set USART1 TX to send data
USART1_CR1bits.RXNEIE = 1; //Enable RX interrupt
UART1_Write_Text("INTERRUPT TEST\n\n");
UART_Write(10);
UART_Write(13);
NVIC_IntEnable(IVT_INT_USART1);
Enableinterrupts();
while(1)
{
display();
if(header==1) // Just for checking in header or not
Lcd_Out(3,1,"1");
else
Lcd_Out(3,1,"0");
Fan_RPM[0]=(check_sum/1000)+48;
Fan_RPM[1]=((check_sum/100)%10)+48;
Fan_RPM[2]=((check_sum/10)%10)+48;
Fan_RPM[3]=((check_sum)%10)+48;
Lcd_Out(4,1,Fan_RPM);
Fan_RPM[0]=(i/1000)+48;
Fan_RPM[1]=((i/100)%10)+48;
Fan_RPM[2]=((i/10)%10)+48;
Fan_RPM[3]=((i)%10)+48;
Lcd_Out(4,10,Fan_RPM);
} // end while
} // End main