Saturday 2 September 2017

STM8S103F3P6

20170905.2

Items:
1  PC    (Windows 7)
2  Internet access
3  ST-Link V2    US$ 2.57         
4  STM8S103F3P6 Minimum System Development Board    US$ 1.68 for 2 pieces
       This is quite small, measuring only 30 mm by 18 mm.
                                             
5  IDE    (IAR Embedded Workbench 3.10)
6  Breadboard, connectors, LEDs, resistors etc
7  74HC164
8  LCD 1602   (Module Display For Arduino)

Setup:
Read STM8S Reference Manual
Read STM8S103 Datasheet
Install IDE  
Connect the STM8S103 to the ST-LinkV2 (3V3 or 5V)
Plug the ST-LinkV2 to the PC
                                                           



Programming:
Launch the IDE
Create a new empty project with STM8S103F3P as the device,
and ST-Link as the Debugger.
Type in a program, name it main.c and add it to the project
Make and 'Download and Debug'
(Click) Go and check that the STM8S103 behaves as in the program.    


Approach:
IDE usage and downloading
Simple output
Interrupt
SPI output
LCD 1602


Notes on the Examples:
T10          Test IDE and downloading
T13p        Turn on LED (connected to PB5)
                                                     

T15p        Blink LED
T17c        Change to full 16-MHz speed
T22t        Test Timer 4   (Dim LED)
                                                         

T23t        Control LED with Timer4
T26i        Timer 4 Interrupt 

       




Examples:
==========
// T10.c

int main(){
 
  return 0;
}
==========
// T13p.c

#include <iostm8s103f3.h>

int main(){
 
  PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on   
  PB_CR1  |=   1<<5 ;   // bit 5  Push-Pull
  PB_CR2  &= ~(1<<5) ;  // bit 5  Slow (2MHz)
  PB_DDR  |=   1<<5 ;   // bit 5  output  

  while(1);
}
==========
// T15p.c

#include <iostm8s103f3.h>

int main(){
  int i ; 
 
  PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on   
  PB_CR1  |=   1<<5 ;   // bit 5  Push-Pull
  PB_CR2  &= ~(1<<5) ;  // bit 5  Slow (2MHz)
  PB_DDR  |=   1<<5 ;   // bit 5  output  
 
while(1){
    PB_ODR  &= ~(1<<5) ;        // bit 5  clear  LED on
    for(i=0;i<=30000;i++){ ; }  // Software delay
    PB_ODR  |=  (1<<5) ;        // bit 5  set    LED off   
    for(i=0;i<=30000;i++){ ; }  // Software delay
  }
  //while(1);
}
==========
// T17c.c

#include <iostm8s103f3.h>

int main(){
  int i,j ; 
  CLK_CKDIVR &= ~MASK_CLK_CKDIVR_HSIDIV ;       // Mask /0 16-MHz
 
  PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on   
  PB_CR1  |=   1<<5 ;   // bit 5  Push-Pull
  PB_CR2  &= ~(1<<5) ;  // bit 5  Slow (2MHz)
  PB_DDR  |=   1<<5 ;   // bit 5  output  
 
while(1){
    PB_ODR  &= ~(1<<5) ;        // bit 5  clear  LED on
    for(i=0;i<=1000;i++){for(j=0;j<=1000;j++);} 
    PB_ODR  |=  (1<<5) ;        // bit 5  set    LED off   
    for(i=0;i<=1000;i++){for(j=0;j<=1000;j++);} 
  }
  //while(1);
}
==========
// T22t.c

#include <iostm8s103f3.h>

int main(){
 
  CLK_CKDIVR &= ~MASK_CLK_CKDIVR_HSIDIV ;       // Mask /0 16-MHz
 
  PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on   
  PB_CR1  |=   1<<5 ;   // bit 5  Push-Pull
  PB_CR2  &= ~(1<<5) ;  // bit 5  Slow (2MHz)
  PB_DDR  |=   1<<5 ;   // bit 5  output  
 
  TIM4_ARR = 0xFF ;
  TIM4_PSCR |= MASK_TIM4_PSCR_PSC ;     // 7  /128
  TIM4_CR1  |= MASK_TIM4_CR1_CEN ;
 
  while(1){                                     // 0xF8 --> dim
    if( TIM4_CNTR>=0xF8 ) PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on
                  else    PB_ODR  |=  (1<<5) ;  // bit 5  set    LED off  
  }
  //while(1);
}
==========
// T23t.c

#include <iostm8s103f3.h>

int main(){
  int i ;
 
  CLK_CKDIVR &= ~MASK_CLK_CKDIVR_HSIDIV ;       // Mask /0 16-MHz
 
  PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on   
  PB_CR1  |=   1<<5 ;   // bit 5  Push-Pull
  PB_CR2  &= ~(1<<5) ;  // bit 5  Slow (2MHz)
  PB_DDR  |=   1<<5 ;   // bit 5  output  
 
  TIM4_ARR = 0xFF ;
  TIM4_PSCR |= MASK_TIM4_PSCR_PSC ;     // (7)  /128
  TIM4_CR1  |= MASK_TIM4_CR1_CEN ;
 
  i = 0 ;
  while(1){
    while( (TIM4_SR&MASK_TIM4_SR_UIF)==0 ) ;    // Wait till overflow
    TIM4_SR &= ~MASK_TIM4_SR_UIF ;              // Clear flag
    i++ ;
    if( i<=500 ) PB_ODR  = 0x00 ;
          else   PB_ODR  = 0x20 ;
    if( i==700 ) i = 0 ;
  }
  //while(1);
}
==========
// T26i.c

#include <iostm8s103f3.h>
#include <intrinsics.h>
int i ;

int main(){
 
  CLK_CKDIVR &= ~MASK_CLK_CKDIVR_HSIDIV ;       // Mask /0 16-MHz
 
  PB_ODR  &= ~(1<<5) ;  // bit 5  clear  LED on   
  PB_CR1  |=   1<<5 ;   // bit 5  Push-Pull
  PB_CR2  &= ~(1<<5) ;  // bit 5  Slow (2MHz)
  PB_DDR  |=   1<<5 ;   // bit 5  output  
 
  TIM4_ARR = 0xFF ;
  TIM4_PSCR |= MASK_TIM4_PSCR_PSC ;     // (7)  /128
  TIM4_CR1  |= MASK_TIM4_CR1_CEN ;
  TIM4_SR &= ~MASK_TIM4_SR_UIF ;        // Clear flag
  TIM4_IER  |= MASK_TIM4_IER_UIE ;
  __enable_interrupt();
   
  i = 0 ;
  while(1){;}   // Wait for interrupt
  //while(1);
}
#pragma vector = TIM4_OVR_UIF_vector
__interrupt void Timer4ISR(void){
  TIM4_SR &= ~MASK_TIM4_SR_UIF ;        // Clears Timer 4 Interrupt Flag
  i++ ;
    if( i<=500 ) PB_ODR  = 0x00 ;
          else   PB_ODR  = 0x20 ;
    if( i==1000 ) i = 0 ;
  }
==========