2018-10-25 05:57 AM
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32l0xx_hal.h"
/* USER CODE BEGIN Includes */
/*LCD RST */
# define LCD_RST1 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
# define LCD_RST0 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
/*LCD CS */
# define LCD_CS1 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET);
# define LCD_CS0 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET);
/* LCD RS */
# define LCD_RS1 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET);//A0
# define LCD_RS0 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET);
/* LCD SCK */
# define LCD_SCK1 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);//SCK
# define LCD_SCK0 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/* LCD MOSI */
# define LCD_MOSI1 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);//SDA
# define LCD_MOSI0 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t aTxBuffer[2];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
/**
* @brief The application entry point.
*
* @retval None
*/
void lcd_send_cmd(unsigned char cmd)
{
aTxBuffer[0]= (cmd&0x0f)|0x04;
aTxBuffer[1]= (cmd&0x0f);
LCD_RS0;
LCD_CS0;
LCD_SCK0;
for(int i=0;i<8;i++){
LCD_SCK0;
if( cmd & 0x0f){
LCD_MOSI1;}
else
{LCD_MOSI0;}
LCD_SCK1;
HAL_Delay(100);
// LCD_SCK0;
cmd<<=1;
}
HAL_SPI_Transmit(&hspi1,&cmd,2,100);
while (HAL_SPI_GetState(&hspi1)!= HAL_SPI_STATE_READY);
LCD_MOSI0;
LCD_CS1;
}
void lcd_send_data(unsigned char data)
{
aTxBuffer[0]= (data&0x0f)|0x04;
aTxBuffer[1]= (data&0x0f);
LCD_RS1;
LCD_CS0;
LCD_SCK0;
for(int i=0;i<8;i++)
{
LCD_SCK0;
HAL_Delay(100);
if (data & 0x0f)
{LCD_MOSI1;}
else {LCD_MOSI0;}
LCD_SCK1;
HAL_Delay(100);
data<<=1;
}
HAL_SPI_Transmit(&hspi1,&data,2,100);
while (HAL_SPI_GetState(&hspi1)!= HAL_SPI_STATE_READY);
LCD_MOSI0;
LCD_CS1;
}
void set_rr(char rr) //0<=rr<=7
{
LCD_RS0;
lcd_send_data((0x20)|((rr)&(0x07)));
}
void set_ev(char ev) //0<=ev<=63
{
LCD_RS0;
lcd_send_data(0x81);
lcd_send_data(((0x3F)&(ev)));
}
void power_control(char dat) //0<=dat<=7
//dat=1: Built-in Follower ON
//dat=2: Built-in Regulator ON
//dat=4: Built-in Booster ON
{
LCD_RS0;
dat=((dat)&(0x07));
lcd_send_data(((0x28)|(dat)));
}
// update lcd
void copy_to_lcd(void)
{
for (int page = 0; page < 8; page++) {
lcd_send_cmd(0x00); // set column low nibble 0
lcd_send_cmd(0x10); // set column hi nibble 0
lcd_send_cmd(0xB0|page); // set page address 0
LCD_RS1;
for(int i=page*128; i<(page*128+128); i++) {
lcd_send_data(aTxBuffer[i]);
}
}
}
void lcd_Init(void)
{
// LCD_RS0;
//LCD_CS1;
LCD_RST0;
HAL_Delay(100);
LCD_RST1;
lcd_send_cmd (0xE2);//Sw RESWT
HAL_Delay(100);
lcd_send_cmd (0xA2);//LCD bias
HAL_Delay(100);
lcd_send_cmd (0xAF);//display on
HAL_Delay(100);
lcd_send_cmd (0xA0);//Segment direction
HAL_Delay(100);
lcd_send_cmd (0xC8);//common direction
HAL_Delay(100);
set_rr(0x04);//Regulation resistor select
HAL_Delay(100);
set_ev(0x1F);;//EV select
HAL_Delay(100);
power_control(0x07); //ALL Power Control ON
HAL_Delay(100);
lcd_send_cmd (0x40);//Initial display line 40
HAL_Delay(100);
lcd_send_cmd (0xB0);//set page address
HAL_Delay(100);
lcd_send_cmd (0x10);//set colmn address MSB
HAL_Delay(100);
lcd_send_cmd (0x00);//set column address LSB
HAL_Delay(100);
lcd_send_cmd (0xAF);//display on
HAL_Delay(100);
lcd_send_cmd (0xA4);//normal display , all pixels on
HAL_Delay(100);
copy_to_lcd();
}
void lcd_send_string (char *str)
{
while (*str) lcd_send_data (*str++);
}
void lcd_set_pixel(int x, int y, int color)
{
int page,col;
int row_in_page;
page = y / 8;
/* Selecting Page */
lcd_send_cmd(0xB0 | page);
/* Selecting Column */
col = ((x & 0xf0) >> 4) | 0x10;
lcd_send_cmd(0x10|col);
lcd_send_cmd(x & 0xf0);
/* Pixel location */
row_in_page = y % 8;
#ifdef CONFIG_LARGE_MEMORY
if (color)
gfx_buf[page][col] |= 1 << row_in_page;
else
gfx_buf[page][col] &= ~(1 << row_in_page);
glcd_write_data(gfx_buf[page][col]);
#else
if (color)
lcd_send_data(1 << row_in_page);
#endif
}
/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
// printf("Hello");
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
LCD_RST0;
HAL_Delay(100);
LCD_RST1;
lcd_Init();
lcd_send_cmd(0xB0);
lcd_send_cmd(0x00);
lcd_send_data(0xFF);
lcd_send_string("Its working");
for (int i = 0; i < 128; i++)
{ for (int j = 0; j < 64; j++)
{lcd_set_pixel(i, j, 1);
}
return 0;}
}