2014年1月27日 星期一

STM8S-Discovery_STM8S003F3-8_STM8S陣列宣告記憶體類型

    8051可以宣告指定記憶體類型,例如code、data、idata、bdata、xdata、pdata等類型,那ST STM8S陣列宣告的方式如下列所示:

1. 由設定的參數指定陣列資料在記憶體存放的位置.
@tiny: designates a memory space for which a one byte address is needed.
@near: designates a memory space for which a two byte address is needed.

範例:
@tiny u8 Array1[1] = {0};
@near u16 Array2[1] = {0x00};


2. 由編譯器決定陣列資料在記憶體存放的位置.
const: The const modifier means that the object to which it is applied is constant.
範例:
const char Array3[1] = {0x00};

2014年1月24日 星期五

STM8S-Discovery_STM8S003F3-7_ST Link接線與MCU Reset問題

在開發的的時候遇到的兩個問題:

    ST STM8S-Discovery開發板上Reset的電容原本的設計是使用10nF,當程式在STM8S-Discovery開發板上開發時,使用ST Link Reset,因為延遲時間夠長,所以沒有問題,不會發生無法啟動的現象.     可是,當在自己的所要開發的產品上,使用ST Link燒錄完成後, 移開ST Link,這時須使用電路Reset時,就會發現延遲時間不夠,MCU無法啟動的現象就發生了,這時將C1的電容由10nF改成10uF就可以了.
    不過,當電容改成10uF後,這時再接上ST Link,使用ST Link經由IDE Reset又會發生不能Reset的現象,只要將C1再改回10nF,這就又可以使用ST Link用IDE Reset了.



    要使用ST Link燒錄程式至開發的產品上時,需要將STM8S-Discovery電路板折斷,用來隔開ST Link與ST MCU之間的供電,不然IDE無法同時辨認兩顆MCU,如果不想折斷STM8S-Discovery,可以移除STM8S-Discovery開發板上ST Link的SB1和SB2電阻.

    如果折斷STM8S-Discovery,將ST Link獨立開來,請依照下圖所示,四根PIN都要接,其中VDD是要由開發的產品供電給ST Link,因為折斷板子的同時,也折斷了USB電纜的供電電路.

    如果沒有折斷STM8S-Discovery,是使用移除ST Link的SB1和SB2電阻的方式,系統還是會由USB電纜供電給ST Link,這時要燒錄自己所開發產品上的MCU,VDD就可以不用接.


2014年1月23日 星期四

STM8S-Discovery_STM8S003F3-6_I2C

    STM8S MCU的I2C是使用Hardware設計,與標準8051的I2C是在Firmware開發,使用I/O PIN模擬I2C Write與Read的波形,是有些差異的,Hardware的I2C要熟讀MCU的Datasheet才有辦法寫出來,不過在傳輸速度上也可以提升,使用Firmware速度上就會受到限制.

1. 整個project的程式檔案目錄如下圖所示.
2.  main.c
/***********************************************/
#include "stm8s_003x.h"
#include "stm8s_type.h"
#include "i2c.h"

u8 ByteRead;
int main(void)
{
enableInterrupts(); //InterruptInit

I2C_Init();

I2C_Write(0xE0, 0xC5, 0x54);

ByteRead = I2C_ReadByte(0xE0, 0x10);

return 0;
}
/***********************************************/

3.  i2c.c
/***********************************************/
#include "stm8s_003x.h"
#include "stm8s_type.h"
#include "i2c.h"

void I2C_Init(void)
{
I2C->CR1 = 0x00; //Disable I2C
I2C->CR2 = 0x04; //Acknowledge enable
I2C->FREQR = 10; //Peripheral clock frequency = 1~24mHz
// I2C->OARH = 0x40; //Addressing mode = 10-bit slave address (7-bit address not acknowledged)
I2C->OARH = 0x00; //Addressing mode = 7-bit slave address (10-bit address not acknowledged)
I2C->OARL = 0x00; //Interface address
I2C->ITR = 0x00; //Interrupt register
I2C->CCRH = 0x80; //I2C master mode selection = Fast mode I2C
I2C->CCRL = 30; //Clock control register (Master mode)
I2C->CR1 = 0x01; //Enable I2C
}

void I2C_Write(u8 Addr, u8 Reg, u8 Data)
{
unsigned char Temp;
unsigned int  i;

I2C->CR2 |= 0x01; //I2C Start Condition
while((I2C->SR1 & 0x01) == 0); //Start condition generated

I2C->DR = Addr; //Device address(write)
while((I2C->SR1 & 0x02) == 0); //Address sent
Temp = I2C->SR3; //Status register

I2C->DR = Reg; //Device register
while((I2C->SR1 & 0x80) == 0); //Data register empty (transmitters)

I2C->DR = Data; //Device data
while((I2C->SR1 & 0x80) == 0); //Data register empty (transmitters)

I2C->CR2 |= 0x02; //I2C Stop Condition
i = 0xFFFF;
while(i--);
}

u8 I2C_ReadByte(u8 Addr, u8 Reg)
{
unsigned char Temp, Byte_Read;

I2C->CR2 |= 0x01; //I2C Start Condition
while((I2C->SR1 & 0x01) == 0); //Start condition generated

I2C->DR = Addr; //Device address
while((I2C->SR1 & 0x02) == 0); //Address sent
Temp = I2C->SR3; //Status register

I2C->DR = Reg; //Device register
while((I2C->SR1 & 0x80) == 0); //Data register empty (transmitters)

I2C->CR2 |= 0x01; //I2C Start Condition
while((I2C->SR1 & 0x01) == 0); //Start condition generated

I2C->DR = (Addr | 0x01); //Device address(read)
while((I2C->SR1 & 0x02) == 0); //Address sent
Temp = I2C->SR3; //Status register

I2C->CR2 |= 0x02; //I2C Stop Condition

while((I2C->SR1 & 0x40) == 0); //Byte transfer finished
Byte_Read = 0x00; //Clean data
Byte_Read = I2C->DR;

return Byte_Read;
}

u16 I2C_ReadWord(u8 Addr, u8 Reg)
{
unsigned char Temp, HighByteRead, LowByteRead;
u16 WordRead;

I2C->CR2 |= 0x01; //I2C Start Condition
while((I2C->SR1 & 0x01) == 0); //Start condition generated

I2C->DR = Addr; //Device address
while((I2C->SR1 & 0x02) == 0); //Address sent
Temp = I2C->SR3; //Status register

I2C->DR = Reg; //Device register
while((I2C->SR1 & 0x80) == 0); //Data register empty (transmitters)

I2C->CR2 |= 0x01; //I2C Start Condition
while((I2C->SR1 & 0x01) == 0); //Start condition generated

I2C->DR = (Addr | 0x01); //Device address(read)
while((I2C->SR1 & 0x02) == 0); //Address sent
Temp = I2C->SR3; //Status register

I2C->CR2 |= 0x04; //I2C Ack Condition

while((I2C->SR1 & 0x40) == 0); //Byte transfer finished
HighByteRead = 0x00; //Clean data
HighByteRead = I2C->DR; //Byte 1

I2C->CR2 |= 0x02; //I2C Stop Condition

while((I2C->SR1 & 0x40) == 0); //Byte transfer finished
LowByteRead = 0x00; //Clean data
LowByteRead = I2C->DR; //Byte2

WordRead = (LowByteRead | (HighByteRead << 8));
return WordRead;
}
/***********************************************/


4.  stm8_interrupt_vector.c
/***********************************************/
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
 * Copyright (c) 2007 STMicroelectronics
 */

typedef void @far (*interrupt_handler_t)(void);

struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};

@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
  it is recommended to set a breakpoint on the following instruction
*/
return;
}

extern void _stext();     /* startup routine */

struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap  */
{0x82, NonHandledInterrupt}, /* irq0  */
{0x82, NonHandledInterrupt}, /* irq1  */
{0x82, NonHandledInterrupt}, /* irq2  */
{0x82, NonHandledInterrupt}, /* irq3  */
{0x82, NonHandledInterrupt}, /* irq4  */
{0x82, NonHandledInterrupt}, /* irq5  */
{0x82, NonHandledInterrupt}, /* irq6  */
{0x82, NonHandledInterrupt}, /* irq7  */
{0x82, NonHandledInterrupt}, /* irq8  */
{0x82, NonHandledInterrupt}, /* irq9  */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
/***********************************************/

5.  i2c.h
/***********************************************/
#include "stm8s_type.h"

void I2C_Init(void);
void I2C_Write(u8 Addr, u8 Reg, u8 Data);
u8 I2C_ReadByte(u8 Addr, u8 Reg);
u16 I2C_ReadWord(u8 Addr, u8 Reg);
/***********************************************/

6.  stm8s_003x.h
/***********************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8_003x_H
#define __STM8_003x_H

#include "stm8s_type.h"
/******************************************************************************/
/*                   Library configuration section                            */
/******************************************************************************/

/******************************************************************************/
/*                          Peripherals Base Address                          */
/******************************************************************************/
#define GPIOA_BaseAddress       0x5000
#define GPIOB_BaseAddress       0x5005
#define GPIOC_BaseAddress       0x500A
#define GPIOD_BaseAddress       0x500F
#define GPIOE_BaseAddress       0x5014
#define GPIOF_BaseAddress       0x5019
#define FLASH_BaseAddress       0x505A
#define ITC_BaseAddress         0x50A0
#define RST_BaseAddress         0x50B3
#define CLK_BaseAddress         0x50C3
#define WWDG_BaseAddress       0x50D1
#define IWDG_BaseAddress       0x50E0
#define AWU_BaseAddress         0x50F0
#define BEEP_BaseAddress        0x50F3
#define SPI_BaseAddress         0x5200
#define I2C_BaseAddress         0x5210
#define UART1_BaseAddress       0x5230
#define TIM1_BaseAddress        0x5250
#define TIM2_BaseAddress        0x5300
#define TIM4_BaseAddress        0x5340
#define ADC1_BaseAddress        0x53E0


/******************************************************************************/
/*                          Peripherals declarations                          */
/******************************************************************************/

#define GPIOA ((GPIO_TypeDef *) GPIOA_BaseAddress)
#define GPIOB ((GPIO_TypeDef *) GPIOB_BaseAddress)
#define GPIOC ((GPIO_TypeDef *) GPIOC_BaseAddress)
#define GPIOD ((GPIO_TypeDef *) GPIOD_BaseAddress)
#define GPIOE ((GPIO_TypeDef *) GPIOE_BaseAddress)
#define GPIOF  ((GPIO_TypeDef *)  GPIOF_BaseAddress)
#define FLASH ((FLASH_TypeDef *) FLASH_BaseAddress)
#define ITC ((ITC_TypeDef *) ITC_BaseAddress)
#define RST ((RST_TypeDef *) RST_BaseAddress)
#define CLK ((CLK_TypeDef *) CLK_BaseAddress)
#define WWDG ((WWDG_TypeDef *) WWDG_BaseAddress)
#define IWDG ((IWDG_TypeDef *) IWDG_BaseAddress)
#define AWU ((AWU_TypeDef *) AWU_BaseAddress)
#define BEEP ((BEEP_TypeDef *) BEEP_BaseAddress)
#define SPI ((SPI_TypeDef *) SPI_BaseAddress)
#define I2C ((I2C_TypeDef *) I2C_BaseAddress)
#define UART1 ((UART1_TypeDef *) UART1_BaseAddress)
#define TIM1 ((TIM1_TypeDef *) TIM1_BaseAddress)
#define TIM2 ((TIM2_TypeDef *) TIM2_BaseAddress)
#define TIM4 ((TIM4_TypeDef *) TIM4_BaseAddress)
#define ADC1 ((ADC1_TypeDef *) ADC1_BaseAddress)

/******************************************************************************/
/*                          IP registers structures                           */
/******************************************************************************/

#define enableInterrupts() {_asm("rim\n");} /* enable interrupts */
#define disableInterrupts() {_asm("sim\n");} /* disable interrupts */

/* @brief Inter-Integrated Circuit (I2C) */

typedef struct I2C_struct
{
  vu8 CR1;       /*!< I2C control register 1 */
  vu8 CR2;       /*!< I2C control register 2 */
  vu8 FREQR;     /*!< I2C frequency register */
  vu8 OARL;      /*!< I2C own address register LSB */
  vu8 OARH;      /*!< I2C own address register MSB */
  vu8 RESERVED1; /*!< Reserved byte */
  vu8 DR;        /*!< I2C data register */
  vu8 SR1;       /*!< I2C status register 1 */
  vu8 SR2;       /*!< I2C status register 2 */
  vu8 SR3;       /*!< I2C status register 3 */
  vu8 ITR;       /*!< I2C interrupt register */
  vu8 CCRL;      /*!< I2C clock control register low */
  vu8 CCRH;      /*!< I2C clock control register high */
  vu8 TRISER;    /*!< I2C maximum rise time register */
  vu8 PECR; /*!< I2C packet error checking register */
}I2C_TypeDef;

#endif /* __STM8S_003x_H */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

/***********************************************/

7.  stm8s_type.h
/***********************************************/
/**
  ******************************************************************************
  * @file stm8s_type.h
  * @brief This file contains all common data types.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.1.1
  * @date 06/05/2009
  ******************************************************************************
  *
  * 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>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8S_TYPE_H
#define __STM8S_TYPE_H

/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
typedef signed long  s32;
typedef signed short s16;
typedef signed char  s8;

typedef signed long  const sc32;  /* Read Only */
typedef signed short const sc16;  /* Read Only */
typedef signed char  const sc8;   /* Read Only */

typedef volatile signed long  vs32;
typedef volatile signed short vs16;
typedef volatile signed char  vs8;

typedef volatile signed long  const vsc32;  /* Read Only */
typedef volatile signed short const vsc16;  /* Read Only */
typedef volatile signed char  const vsc8;   /* Read Only */

typedef unsigned long  u32;
typedef unsigned short u16;
typedef unsigned char  u8;

typedef unsigned long  const uc32;  /* Read Only */
typedef unsigned short const uc16;  /* Read Only */
typedef unsigned char  const uc8;   /* Read Only */

typedef volatile unsigned long  vu32;
typedef volatile unsigned short vu16;
typedef volatile unsigned char  vu8;

typedef volatile unsigned long  const vuc32;  /* Read Only */
typedef volatile unsigned short const vuc16;  /* Read Only */
typedef volatile unsigned char  const vuc8;   /* Read Only */

typedef enum
{
  FALSE = 0,
  TRUE = !FALSE
}bool;

typedef enum
{
  RESET = 0,
  SET = !RESET
}FlagStatus, ITStatus, BitStatus;

typedef enum
{
  DISABLE = 0,
  ENABLE = !DISABLE
}FunctionalState;

#define IS_FUNCTIONALSTATE_OK(VALUE) ( (VALUE == ENABLE) || (VALUE == DISABLE) )

typedef enum
{
  ERROR = 0,
  SUCCESS = !ERROR
}ErrorStatus;

#define U8_MAX     ((u8)255)
#define S8_MAX     ((s8)127)
#define S8_MIN     ((s8)-128)
#define U16_MAX    ((u16)65535u)
#define S16_MAX    ((s16)32767)
#define S16_MIN    ((s16)-32768)
#define U32_MAX    ((u32)4294967295uL)
#define S32_MAX    ((s32)2147483647)
#define S32_MIN    ((s32)-2147483648)

/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

#endif /* __STM8S_TYPE_H */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

/***********************************************/

STM8S-Discovery_STM8S003F3-5_UART使用printf()

    之前建立一個新的project是使用STM8105C6這顆MCU,不過因為某些原因所以換成STM8103F3,這兩顆在中斷位置與暫存器位置上有些許差異,所以程式在不同MCU移植時,請先檢查比對Datasheet的Register map(修改stm8s_003x.h)與Interrupt vector mapping(修改stm8_interrupt_vector.c).

    STM8S的編譯器Comic支援system call所以UART初始化後,UART的Tx可以直接使用printf(),那Rx可以使用char getinput(void);函數或是讀取flag的值.

1. 整個project的程式檔案目錄如下圖所示.

2.  main.c
/***********************************************/
#include "stm8s_003x.h"
#include "stm8s_type.h"
#include "uart.h"
#include <stdio.h>

u8 i = 10;
int main(void)
{
  UART_CLKInit();
  UART_GPIOInit();
  UART1_Init();

enableInterrupts(); //InterruptInit

printf("Hello World.\n\r");
printf("Value = %d \n\r", i);

return 0;
}

@far @interrupt void UART1_RX_IRQHandler (void)
{
  flag = UART1->DR;  //Auto clear RXNE bit

  UART1->SR &= 0xDF; //Clear RXNE by write 0 to it
}

@far @interrupt void UART1_TX_IRQHandler (void)
{
flag = UART1->DR;
}
/***********************************************/

3. stm8_interrupt_vector.c
/***********************************************/
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
 * Copyright (c) 2007 STMicroelectronics
 */

typedef void @far (*interrupt_handler_t)(void);

struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};

@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
  it is recommended to set a breakpoint on the following instruction
*/
return;
}

extern void _stext();     /* startup routine */
extern @far @interrupt void UART1_TX_IRQHandler (void);
extern @far @interrupt void UART1_RX_IRQHandler (void);


struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap  */
{0x82, NonHandledInterrupt}, /* irq0  */
{0x82, NonHandledInterrupt}, /* irq1  */
{0x82, NonHandledInterrupt}, /* irq2  */
{0x82, NonHandledInterrupt}, /* irq3  */
{0x82, NonHandledInterrupt}, /* irq4  */
{0x82, NonHandledInterrupt}, /* irq5  */
{0x82, NonHandledInterrupt}, /* irq6  */
{0x82, NonHandledInterrupt}, /* irq7  */
{0x82, NonHandledInterrupt}, /* irq8  */
{0x82, NonHandledInterrupt}, /* irq9  */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, (interrupt_handler_t)UART1_TX_IRQHandler}, /* irq17 */
{0x82, (interrupt_handler_t)UART1_RX_IRQHandler}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
/***********************************************/

4. uart.c
/***********************************************/
#include "stm8s_003x.h"
#include "stm8s_type.h"

void UART_CLKInit(void) //initialize the registers of Clock 
{
  CLK->ECKR |= 0x01; //enable extern clock,only ECKR_HSEEN = 1 
  
  CLK->CKDIVR = 0x00; //set prescaler
  
  CLK->PCKENR1 |= 0x08; //enable peripheral clock, only CLK_PCKENR1_3 = 1; 
}

void UART_GPIOInit() //initialize the registers of GPIO
{
  GPIOD->DDR |= 0x20; //PD5 output, PD6 input
  
  GPIOD->CR1 |= 0x20; //output : push-pull mode ; input: float mode
  
  GPIOD->CR2 |= 0x20; //output at 10 Mhz
}

void UART1_Init(void) //initialize the registers of uart2
{
  
//  UART1->BRR2 = 0x02; //set baud rate at 1200 ; mainClock : 16 000 000   
//  UART1->BRR1 = 0x68;
UART1->BRR2 = 0x08; //set baud rate at 19200 ; mainClock : 16 000 000  
  UART1->BRR1 = 0x06;
  UART1->CR1 = 0x00; //NO parity PCEN=0,8 data bits M=0,enable UART2 UARTD=0
    
  UART1->CR2 = 0x2C; //wait for you; enable TX and RX TEN=0,REN=0;enable receive interrupt RIEN=1
    
  UART1->CR3 = 0x00; //one stop bits
}

void SendByte(u8 temp) //send one byte by the UART2
{
  while(((UART1->SR)&0x80) != 0x80); //estimate if send OK
UART1->DR = temp;
}

char putchar(char c) //Retargets the C library printf function to the UART2
{
  SendByte((u8)c); //Write a character to the UART1 
  while((UART1->SR & 0x40) != 0x40); //Loop until the end of transmission 
  UART1->SR &= 0xBF; //clear TC bit by write 0 to it
    
  return(c);
}

char getinput(void)
{
volatile char input;
return(input);
}
/***********************************************/

5. stm8s_003x.h
/***********************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8_003x_H
#define __STM8_003x_H

#include "stm8s_type.h"
/******************************************************************************/
/*                   Library configuration section                            */
/******************************************************************************/

/******************************************************************************/
/*                          Peripherals Base Address                          */
/******************************************************************************/
#define GPIOA_BaseAddress       0x5000
#define GPIOB_BaseAddress       0x5005
#define GPIOC_BaseAddress       0x500A
#define GPIOD_BaseAddress       0x500F
#define GPIOE_BaseAddress       0x5014
#define GPIOF_BaseAddress       0x5019
#define FLASH_BaseAddress       0x505A
#define ITC_BaseAddress         0x50A0
#define RST_BaseAddress         0x50B3
#define CLK_BaseAddress         0x50C3
#define WWDG_BaseAddress       0x50D1
#define IWDG_BaseAddress       0x50E0
#define AWU_BaseAddress         0x50F0
#define BEEP_BaseAddress        0x50F3
#define SPI_BaseAddress         0x5200
#define I2C_BaseAddress         0x5210
#define UART1_BaseAddress       0x5230
#define TIM1_BaseAddress        0x5250
#define TIM2_BaseAddress        0x5300
#define TIM4_BaseAddress        0x5340
#define ADC1_BaseAddress        0x53E0


/******************************************************************************/
/*                          Peripherals declarations                          */
/******************************************************************************/

#define GPIOA ((GPIO_TypeDef *) GPIOA_BaseAddress)
#define GPIOB ((GPIO_TypeDef *) GPIOB_BaseAddress)
#define GPIOC ((GPIO_TypeDef *) GPIOC_BaseAddress)
#define GPIOD ((GPIO_TypeDef *) GPIOD_BaseAddress)
#define GPIOE ((GPIO_TypeDef *) GPIOE_BaseAddress)
#define GPIOF  ((GPIO_TypeDef *)  GPIOF_BaseAddress)
#define FLASH ((FLASH_TypeDef *) FLASH_BaseAddress)
#define ITC ((ITC_TypeDef *) ITC_BaseAddress)
#define RST ((RST_TypeDef *) RST_BaseAddress)
#define CLK ((CLK_TypeDef *) CLK_BaseAddress)
#define WWDG ((WWDG_TypeDef *) WWDG_BaseAddress)
#define IWDG ((IWDG_TypeDef *) IWDG_BaseAddress)
#define AWU ((AWU_TypeDef *) AWU_BaseAddress)
#define BEEP ((BEEP_TypeDef *) BEEP_BaseAddress)
#define SPI ((SPI_TypeDef *) SPI_BaseAddress)
#define I2C ((I2C_TypeDef *) I2C_BaseAddress)
#define UART1 ((UART1_TypeDef *) UART1_BaseAddress)
#define TIM1 ((TIM1_TypeDef *) TIM1_BaseAddress)
#define TIM2 ((TIM2_TypeDef *) TIM2_BaseAddress)
#define TIM4 ((TIM4_TypeDef *) TIM4_BaseAddress)
#define ADC1 ((ADC1_TypeDef *) ADC1_BaseAddress)

/******************************************************************************/
/*                          IP registers structures                           */
/******************************************************************************/

#define enableInterrupts() {_asm("rim\n");} /* enable interrupts */
#define disableInterrupts() {_asm("sim\n");} /* disable interrupts */
/* @brief General Purpose I/Os (GPIO) */
typedef struct GPIO_struct
{
vu8 ODR; /*!< Output Data Register */
  vu8 IDR; /*!< Input Data Register */
  vu8 DDR; /*!< Data Direction Register */
  vu8 CR1; /*!< Configuration Register 1 */
  vu8 CR2; /*!< Configuration Register 2 */
}GPIO_TypeDef;

/* @brief Clock Controller (CLK) */

typedef struct CLK_struct
{
  vu8 ICKR;     /*!< Internal Clocks Control Register */
  vu8 ECKR;     /*!< External Clocks Control Register */
  u8 RESERVED; /*!< Reserved byte */
  vu8 CMSR;     /*!< Clock Master Status Register */
  vu8 SWR;      /*!< Clock Master Switch Register */
  vu8 SWCR;     /*!< Switch Control Register */
  vu8 CKDIVR;   /*!< Clock Divider Register */
  vu8 PCKENR1;  /*!< Peripheral Clock Gating Register 1 */
  vu8 CSSR;     /*!< Clock Security Sytem Register */
  vu8 CCOR;     /*!< Configurable Clock Output Register */
  vu8 PCKENR2;  /*!< Peripheral Clock Gating Register 2 */
  vu8 CANCCR;   /*!< CAN external clock control Register (exist only in STM8S208 otherwise it is reserved) */
  vu8 HSITRIMR; /*!< HSI Calibration Trimmer Register */
  vu8 SWIMCCR;  /*!< SWIM clock control register */
}CLK_TypeDef;

/* @brief Universal Synchronous Asynchronous Receiver Transmitter (UART1) */

typedef struct UART1_struct
{
  vu8 SR;   /*!< UART1 status register */
  vu8 DR;   /*!< UART1 data register */
  vu8 BRR1; /*!< UART1 baud rate register */
  vu8 BRR2; /*!< UART1 DIV mantissa[11:8] SCIDIV fraction */
  vu8 CR1;  /*!< UART1 control register 1 */
  vu8 CR2;  /*!< UART1 control register 2 */
  vu8 CR3;  /*!< UART1 control register 3 */
  vu8 CR4;  /*!< UART1 control register 4 */
  vu8 CR5;  /*!< UART1 control register 5 */
vu8 CR6;  /*!< UART1 control register 6 */
  vu8 GTR;  /*!< UART1 guard time register */
  vu8 PSCR; /*!< UART1 prescaler register */
}UART1_TypeDef;


#endif /* __STM8S_003x_H */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

/***********************************************/

6. stm8s_type.h
/***********************************************/
/**
  ******************************************************************************
  * @file stm8s_type.h
  * @brief This file contains all common data types.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.1.1
  * @date 06/05/2009
  ******************************************************************************
  *
  * 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>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8S_TYPE_H
#define __STM8S_TYPE_H

/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
typedef signed long  s32;
typedef signed short s16;
typedef signed char  s8;

typedef signed long  const sc32;  /* Read Only */
typedef signed short const sc16;  /* Read Only */
typedef signed char  const sc8;   /* Read Only */

typedef volatile signed long  vs32;
typedef volatile signed short vs16;
typedef volatile signed char  vs8;

typedef volatile signed long  const vsc32;  /* Read Only */
typedef volatile signed short const vsc16;  /* Read Only */
typedef volatile signed char  const vsc8;   /* Read Only */

typedef unsigned long  u32;
typedef unsigned short u16;
typedef unsigned char  u8;

typedef unsigned long  const uc32;  /* Read Only */
typedef unsigned short const uc16;  /* Read Only */
typedef unsigned char  const uc8;   /* Read Only */

typedef volatile unsigned long  vu32;
typedef volatile unsigned short vu16;
typedef volatile unsigned char  vu8;

typedef volatile unsigned long  const vuc32;  /* Read Only */
typedef volatile unsigned short const vuc16;  /* Read Only */
typedef volatile unsigned char  const vuc8;   /* Read Only */

typedef enum
{
  FALSE = 0,
  TRUE = !FALSE
}bool;

typedef enum 
{
  RESET = 0,
  SET = !RESET
}FlagStatus, ITStatus, BitStatus;

typedef enum
{
  DISABLE = 0,
  ENABLE = !DISABLE
}FunctionalState;

#define IS_FUNCTIONALSTATE_OK(VALUE) ( (VALUE == ENABLE) || (VALUE == DISABLE) )

typedef enum
{
  ERROR = 0,
  SUCCESS = !ERROR
}ErrorStatus;

#define U8_MAX     ((u8)255)
#define S8_MAX     ((s8)127)
#define S8_MIN     ((s8)-128)
#define U16_MAX    ((u16)65535u)
#define S16_MAX    ((s16)32767)
#define S16_MIN    ((s16)-32768)
#define U32_MAX    ((u32)4294967295uL)
#define S32_MAX    ((s32)2147483647)
#define S32_MIN    ((s32)-2147483648)

/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

#endif /* __STM8S_TYPE_H */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

/***********************************************/

7. uart.h
/***********************************************/
#include "stm8s_type.h"

void UART_CLKInit(void);
void UART_GPIOInit(void);
void UART1_Init(void);
void SendByte(u8 temp);
char putchar(char c);
char getinput(void);

u8 flag = 0;
/***********************************************/