Ср июн 21, 2017 12:12:36
Ср июн 21, 2017 23:25:02
Чт июл 06, 2017 22:38:57
Вс июл 09, 2017 12:24:10
Сб июл 22, 2017 00:24:40
Сб июл 22, 2017 18:29:55
Сб июл 22, 2017 19:58:59
Пт авг 18, 2017 11:52:58
Чт окт 19, 2017 20:08:49
Пт окт 20, 2017 18:34:07
Вт окт 24, 2017 15:37:11
Пн янв 08, 2018 16:53:33
/**
******************************************************************************
* @file Project/main.c
* @author MCD Application Team
* @version V2.1.0
* @date 18-November-2011
* @brief Main program body
******************************************************************************
* @attention
*
* 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.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ----------*/
#include "stm8s.h"
#include "string.h"
/* Private defines ----------*/
static unsigned char const Font5x7[] = {
0x00, 0x00, 0x00, 0x00, 0x00, // (space)
0x00, 0x00, 0x5F, 0x00, 0x00, // !
0x00, 0x07, 0x00, 0x07, 0x00, // "
0x14, 0x7F, 0x14, 0x7F, 0x14, // #
0x24, 0x2A, 0x7F, 0x2A, 0x12, // $
0x23, 0x13, 0x08, 0x64, 0x62, // %
0x36, 0x49, 0x55, 0x22, 0x50, // &
0x00, 0x05, 0x03, 0x00, 0x00, // '
0x00, 0x1C, 0x22, 0x41, 0x00, // (
0x00, 0x41, 0x22, 0x1C, 0x00, // )
0x08, 0x2A, 0x1C, 0x2A, 0x08, // *
0x08, 0x08, 0x3E, 0x08, 0x08, // +
0x00, 0x50, 0x30, 0x00, 0x00, // ,
0x08, 0x08, 0x08, 0x08, 0x08, // -
0x00, 0x60, 0x60, 0x00, 0x00, // .
0x20, 0x10, 0x08, 0x04, 0x02, // /
0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
0x00, 0x42, 0x7F, 0x40, 0x00, // 1
0x42, 0x61, 0x51, 0x49, 0x46, // 2
0x21, 0x41, 0x45, 0x4B, 0x31, // 3
0x18, 0x14, 0x12, 0x7F, 0x10, // 4
0x27, 0x45, 0x45, 0x45, 0x39, // 5
0x3C, 0x4A, 0x49, 0x49, 0x30, // 6
0x01, 0x71, 0x09, 0x05, 0x03, // 7
0x36, 0x49, 0x49, 0x49, 0x36, // 8
0x06, 0x49, 0x49, 0x29, 0x1E, // 9
0x00, 0x36, 0x36, 0x00, 0x00, // :
0x00, 0x56, 0x36, 0x00, 0x00, // ;
0x00, 0x08, 0x14, 0x22, 0x41, // <
0x14, 0x14, 0x14, 0x14, 0x14, // =
0x41, 0x22, 0x14, 0x08, 0x00, // >
0x02, 0x01, 0x51, 0x09, 0x06, // ?
0x32, 0x49, 0x79, 0x41, 0x3E, // @
0x7E, 0x11, 0x11, 0x11, 0x7E, // A
0x7F, 0x49, 0x49, 0x49, 0x36, // B
0x3E, 0x41, 0x41, 0x41, 0x22, // C
0x7F, 0x41, 0x41, 0x22, 0x1C, // D
0x7F, 0x49, 0x49, 0x49, 0x41, // E
0x7F, 0x09, 0x09, 0x01, 0x01, // F
0x3E, 0x41, 0x41, 0x51, 0x32, // G
0x7F, 0x08, 0x08, 0x08, 0x7F, // H
0x00, 0x41, 0x7F, 0x41, 0x00, // I
0x20, 0x40, 0x41, 0x3F, 0x01, // J
0x7F, 0x08, 0x14, 0x22, 0x41, // K
0x7F, 0x40, 0x40, 0x40, 0x40, // L
0x7F, 0x02, 0x04, 0x02, 0x7F, // M
0x7F, 0x04, 0x08, 0x10, 0x7F, // N
0x3E, 0x41, 0x41, 0x41, 0x3E, // O
0x7F, 0x09, 0x09, 0x09, 0x06, // P
0x3E, 0x41, 0x51, 0x21, 0x5E, // Q
0x7F, 0x09, 0x19, 0x29, 0x46, // R
0x46, 0x49, 0x49, 0x49, 0x31, // S
0x01, 0x01, 0x7F, 0x01, 0x01, // T
0x3F, 0x40, 0x40, 0x40, 0x3F, // U
0x1F, 0x20, 0x40, 0x20, 0x1F, // V
0x7F, 0x20, 0x18, 0x20, 0x7F, // W
0x63, 0x14, 0x08, 0x14, 0x63, // X
0x03, 0x04, 0x78, 0x04, 0x03, // Y
0x61, 0x51, 0x49, 0x45, 0x43, // Z
0x00, 0x00, 0x7F, 0x41, 0x41, // [
0x02, 0x04, 0x08, 0x10, 0x20, // "\"
0x41, 0x41, 0x7F, 0x00, 0x00, // ]
0x04, 0x02, 0x01, 0x02, 0x04, // ^
0x40, 0x40, 0x40, 0x40, 0x40, // _
0x00, 0x01, 0x02, 0x04, 0x00, // `
0x20, 0x54, 0x54, 0x54, 0x78, // a
0x7F, 0x48, 0x44, 0x44, 0x38, // b
0x38, 0x44, 0x44, 0x44, 0x20, // c
0x38, 0x44, 0x44, 0x48, 0x7F, // d
0x38, 0x54, 0x54, 0x54, 0x18, // e
0x08, 0x7E, 0x09, 0x01, 0x02, // f
0x08, 0x14, 0x54, 0x54, 0x3C, // g
0x7F, 0x08, 0x04, 0x04, 0x78, // h
0x00, 0x44, 0x7D, 0x40, 0x00, // i
0x20, 0x40, 0x44, 0x3D, 0x00, // j
0x00, 0x7F, 0x10, 0x28, 0x44, // k
0x00, 0x41, 0x7F, 0x40, 0x00, // l
0x7C, 0x04, 0x18, 0x04, 0x78, // m
0x7C, 0x08, 0x04, 0x04, 0x78, // n
0x38, 0x44, 0x44, 0x44, 0x38, // o
0x7C, 0x14, 0x14, 0x14, 0x08, // p
0x08, 0x14, 0x14, 0x18, 0x7C, // q
0x7C, 0x08, 0x04, 0x04, 0x08, // r
0x48, 0x54, 0x54, 0x54, 0x20, // s
0x04, 0x3F, 0x44, 0x40, 0x20, // t
0x3C, 0x40, 0x40, 0x20, 0x7C, // u
0x1C, 0x20, 0x40, 0x20, 0x1C, // v
0x3C, 0x40, 0x30, 0x40, 0x3C, // w
0x44, 0x28, 0x10, 0x28, 0x44, // x
0x0C, 0x50, 0x50, 0x50, 0x3C, // y
0x44, 0x64, 0x54, 0x4C, 0x44, // z
0x00, 0x08, 0x36, 0x41, 0x00, // {
0x00, 0x00, 0x7F, 0x00, 0x00, // |
0x00, 0x41, 0x36, 0x08, 0x00, // }
0x08, 0x08, 0x2A, 0x1C, 0x08, // ->
0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
};
typedef enum {
scr_normal = 0,
scr_CW = 1,
scr_CCW = 2,
scr_180 = 3
} ScrOrientation_TypeDef;
#define HWRST_SET GPIO_WriteHigh(GPIOC, GPIO_PIN_3);
#define HWRST_RESET GPIO_WriteLow(GPIOC, GPIO_PIN_3);
#define ST7735_COMMAND GPIO_WriteLow(GPIOC, GPIO_PIN_4);
#define ST7735_DATA GPIO_WriteHigh(GPIOC, GPIO_PIN_4);
#define ST7735_SELECT GPIO_WriteLow(GPIOC, GPIO_PIN_2);
#define ST7735_UNSELECT GPIO_WriteHigh(GPIOC, GPIO_PIN_2);
const uint16_t scr_width = 132;
const uint16_t scr_height = 132;
const uint16_t OffSetX = 2;
const uint16_t OffSetY = 1;
/* Private function prototypes ----------*/
/* Private functions ----------*/
void ST7735_write(uint8_t data) {
while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET);
SPI_SendData(data);
}
void ST7735_cmd(uint8_t cmd) {
ST7735_COMMAND;
ST7735_write(cmd);
while (SPI_GetFlagStatus(SPI_FLAG_BSY) == SET);
}
void ST7735_data(uint8_t data) {
ST7735_DATA;
ST7735_write(data);
while (SPI_GetFlagStatus(SPI_FLAG_BSY) == SET);
}
void ST7735_AddrSet(uint16_t XS,uint16_t YS,uint16_t XE,uint16_t YE) {
ST7735_cmd(0x2a); // Column address set
ST7735_DATA;
ST7735_write(XS >> 8);
ST7735_write(XS);
ST7735_write(XE >> 8);
ST7735_write(XE);
ST7735_cmd(0x2b); // Row address set
ST7735_DATA;
ST7735_write(YS >> 8);
ST7735_write(YS);
ST7735_write(YE >> 8);
ST7735_write(YE);
ST7735_cmd(0x2c); // Memory write
}
void ST7735_Clear(uint16_t color) {
uint16_t i;
uint8_t CH,CL;
CH = color >> 8;
CL = (uint8_t)color;
ST7735_SELECT;
ST7735_AddrSet(0,0,(scr_width - 1),(scr_height - 1));
ST7735_DATA;
for (i = 0; i < (scr_width * scr_height); i++) {
ST7735_write(CH);
ST7735_write(CL);
}
ST7735_UNSELECT;
}
uint16_t RGB565(uint8_t R,uint8_t G,uint8_t B) {
return ((R >> 3) << 11) | ((G >> 2) << 5) | (B >> 3);
}
void ST7735_PutChar5x7(uint16_t X, uint16_t Y, uint8_t chr, uint16_t color) {
uint16_t i,j;
uint8_t buffer[5];
uint8_t CH = color >> 8;
uint8_t CL = (uint8_t)color;
memcpy(buffer,&Font5x7[(chr - 32) * 5],5);
ST7735_SELECT;
ST7735_AddrSet(X,Y,X + 4,Y + 6);
ST7735_DATA;
for (j = 0; j < 7; j++) {
for (i = 0; i < 5; i++) {
if ((buffer[i] >> j) & 0x01) {
ST7735_write(CH);
ST7735_write(CL);
} else {
ST7735_write(0x00);
ST7735_write(0x00);
}
}
}
ST7735_UNSELECT;
}
void ST7735_PutStr5x7(uint8_t X, uint8_t Y, char *str, uint16_t color) {
X=X+OffSetX;
Y=Y+OffSetY;
while (*str) {
ST7735_PutChar5x7(X,Y,*str++,color);
if (X < scr_width - 6) { X += 6; } else if (Y < scr_height - 8) { X = 0; Y += 8; } else { X = 0; Y = 0; }
};
}
void ST7735_HWRST(){
HWRST_RESET;
HWRST_SET;
}
void ST7735_SWRST(){
ST7735_cmd(0x1);
ST7735_data(0x01);
}
void main(void)
{
CLK_DeInit();
//CLK_HSECmd(DISABLE);
//CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, ENABLE);
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC,GPIO_PIN_3,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOC,GPIO_PIN_4,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOC,GPIO_PIN_2,GPIO_MODE_OUT_PP_HIGH_FAST);
SPI_DeInit();
SPI_Init(SPI_FIRSTBIT_MSB,SPI_BAUDRATEPRESCALER_2,SPI_MODE_MASTER,SPI_CLOCKPOLARITY_HIGH,SPI_CLOCKPHASE_1EDGE,SPI_DATADIRECTION_2LINES_FULLDUPLEX ,SPI_NSS_SOFT,7);
SPI_Cmd(ENABLE);
ST7735_HWRST();
ST7735_SWRST();
ST7735_SELECT;
ST7735_cmd(0x11); // Sleep out & booster on
//Delay_ms(20); // Datasheet says what display wakes about 120ms (may be much faster actually)
//ST7735_cmd(0xb1); // In normal mode (full colors):
//ST7735_DATA;
//ST7735_write(0x05); // RTNA set 1-line period: RTNA2, RTNA0
//ST7735_write(0x3c); // Front porch: FPA5,FPA4,FPA3,FPA2
//ST7735_write(0x3c); // Back porch: BPA5,BPA4,BPA3,BPA2
//ST7735_cmd(0xb2); // In idle mode (8-colors):
//ST7735_DATA;
//ST7735_write(0x05); // RTNB set 1-line period: RTNAB, RTNB0
//ST7735_write(0x3c); // Front porch: FPB5,FPB4,FPB3,FPB2
//ST7735_write(0x3c); // Back porch: BPB5,BPB4,BPB3,BPB2
//ST7735_cmd(0xb3); // In partial mode + full colors:
//ST7735_DATA;
//ST7735_write(0x05); // RTNC set 1-line period: RTNC2, RTNC0
//ST7735_write(0x3c); // Front porch: FPC5,FPC4,FPC3,FPC2
//ST7735_write(0x3c); // Back porch: BPC5,BPC4,BPC3,BPC2
//ST7735_write(0x05); // RTND set 1-line period: RTND2, RTND0
//ST7735_write(0x3c); // Front porch: FPD5,FPD4,FPD3,FPD2
//ST7735_write(0x3c); // Back porch: BPD5,BPD4,BPD3,BPD2
ST7735_cmd(0xB4); // Display dot inversion control:
ST7735_data(0x03); // NLB,NLC
ST7735_cmd(0x3a); // Interface pixel format
// ST7735_data(0x03); // 12-bit/pixel RGB 4-4-4 (4k colors)
ST7735_data(0x05); // 16-bit/pixel RGB 5-6-5 (65k colors)
// ST7735_data(0x06); // 18-bit/pixel RGB 6-6-6 (256k colors)
ST7735_cmd(0x36); // Memory data access control:
// MY MX MV ML RGB MH - -
ST7735_data(0x08);
// ST7735_data(0x00); // Normal: Top to Bottom; Left to Right; RGB
// ST7735_data(0x80); // Y-Mirror: Bottom to top; Left to Right; RGB
// ST7735_data(0x40); // X-Mirror: Top to Bottom; Right to Left; RGB
// ST7735_data(0xc0); // X-Mirror,Y-Mirror: Bottom to top; Right to left; RGB
// ST7735_data(0x20); // X-Y Exchange: X and Y changed positions
// ST7735_data(0xA0); // X-Y Exchange,Y-Mirror
// ST7735_data(0x60); // X-Y Exchange,X-Mirror
// ST7735_data(0xE0); // X-Y Exchange,X-Mirror,Y-Mirror
// ST7735_cmd(0x20); // Display inversion off
// ST7735_cmd(0x21); // Display inversion on
// ST7735_cmd(0x13); // Partial mode off
// ST7735_cmd(0x26); // Gamma curve set:
// ST7735_data(0x01); // Gamma curve 1 (G2.2) or (G1.0)
// ST7735_data(0x02); // Gamma curve 2 (G1.8) or (G2.5)
// ST7735_data(0x04); // Gamma curve 3 (G2.5) or (G2.2)
// ST7735_data(0x08); // Gamma curve 4 (G1.0) or (G1.8)
ST7735_cmd(0x29); // Display on
ST7735_Clear(0x0000);
ST7735_PutStr5x7(0,0,"Hello fucking screen!",RGB565(255,0,0));
ST7735_PutStr5x7(0,10,"This is 5x7 font",RGB565(0,255,0));
ST7735_PutStr5x7(0,20,"Screen 128x160 pixels",RGB565(0,0,255));
ST7735_UNSELECT;
//}
/* Infinite loop */
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : 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 2011 STMicroelectronics *****END OF FILE****/
Сб мар 03, 2018 09:41:13
Сб мар 31, 2018 16:06:19
Сб апр 28, 2018 15:04:50
Ср сен 19, 2018 15:52:13
Пн окт 08, 2018 10:37:59
Вт окт 30, 2018 19:46:59
Чт ноя 08, 2018 11:26:26
Чт дек 06, 2018 15:08:08