Thảo luận Sử dụng mạch nạp j-link V8 và kit STM32F103RBT6

#13
cảm ơn coder đã chia sẻ tài liệu. mình rất vui khi có người cũng nghịch cái kit này.

nhưng bạn gửi file hex không thì hạn chế mọi người quá. bạn có thể chia sẻ tài liệu mà bạn dùng để viết fimware USB được chứ.
 

coder

Sinh viên đại học
#14
Ví dụ Blink 2 con led
PHP:
#include "main.h"

GPIO_InitTypeDef gpioInit;

void init(void);
void delay(unsigned long uDelay);

int main() {
    init();
    while(1) {
        GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        delay(0xfffff);
        GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        delay(0xfffff);
    }
}

void init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &gpioInit);
   
    //gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    //gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    //gpioInit.GPIO_Pin=GPIO_Pin_7/* | GPIO_Pin_9*/;
    //GPIO_Init(GPIOC, &gpioInit);
    //GPIO_ResetBits(GPIOC, GPIO_Pin_7 /*| GPIO_Pin_9*/);
}

void delay(unsigned long uDelay) {
    while(--uDelay);
}
 

coder

Sinh viên đại học
#15
Ví dụ về nút bấm
PHP:
#include "main.h"

GPIO_InitTypeDef gpioInit;

void init(void);
void delay(unsigned long uDelay);

uint8_t a=0;

int main() {
    init();
    while(1) {
        /*
        GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        delay(0xfffff);
        GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        delay(0xfffff);
        */
        if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0)) {
            delay(0xff);
            if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0)) {
                while(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0));
                if(a) {
                    a=0;
                    GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
                } else {
                    a=1;
                    GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
                }
            }
            //GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
        if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)) {
            delay(0xff);
            if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)) {
                while(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1));
                if(a) {
                    a=0;
                    GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
                } else {
                    a=1;
                    GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
                }
            }
            //GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
    }
}

void init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &gpioInit);
   
    gpioInit.GPIO_Mode=GPIO_Mode_IPU;
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1/* | GPIO_Pin_9*/;
    GPIO_Init(GPIOC, &gpioInit);
    //GPIO_ResetBits(GPIOC, GPIO_Pin_7 /*| GPIO_Pin_9*/);
}

void delay(unsigned long uDelay) {
    while(--uDelay);
}
 

coder

Sinh viên đại học
#16
Ví dụ Systick
PHP:
#include "main.h"

GPIO_InitTypeDef gpioInit;

void init(void);
void delay(unsigned long uDelay);

int ktDelay;

int main() {
    init();
    while(1) {
        GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        delay(500);
        GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        delay(500);
    }
}

void init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &gpioInit);
   
    //gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    //gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    //gpioInit.GPIO_Pin=GPIO_Pin_7/* | GPIO_Pin_9*/;
    //GPIO_Init(GPIOC, &gpioInit);
    //GPIO_ResetBits(GPIOC, GPIO_Pin_7 /*| GPIO_Pin_9*/);
    SysTick_Config(SystemCoreClock / 1000);
    //cau hinh xong Systick
   
}

void delay(unsigned long uDelay) {
    ktDelay=uDelay;
    while(ktDelay);
}
===
PHP:
#include "stm32f10x_it.h"

extern int ktDelay;

void SysTick_Handler(void) {
    if(ktDelay) {
        --ktDelay;
    }
}
 

coder

Sinh viên đại học
#17
Ví dụ USART Polling
PHP:
#include "main.h"

GPIO_InitTypeDef gpioInit;
USART_InitTypeDef usartInit;

void init(void);
void delay(unsigned long uDelay);

uint8_t tmp;

int main() {
    init();
    GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
    while(1) {
        //cho den khi nhan duoc 1 byte thi thoi
        while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)==RESET);
        tmp=USART_ReceiveData(USART1);
        //xoa co tran
        USART_ClearFlag(USART1, USART_FLAG_RXNE);
        if(tmp=='a') {
            GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
        if(tmp=='b') {
            GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
       
        USART_SendData(USART1, tmp+2);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
        USART_ClearFlag(USART1, USART_FLAG_TC);
    }
}

void init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &gpioInit);
   
    //khoi tao USART1
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    usartInit.USART_BaudRate=9600;
    usartInit.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    usartInit.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    usartInit.USART_Parity=USART_Parity_No;
    usartInit.USART_StopBits=USART_StopBits_1;
    usartInit.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART1, &usartInit);
    USART_Cmd(USART1, ENABLE);
   
    //khoi tao GPIO PA9 PA10
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Mode=GPIO_Mode_AF_PP;
    gpioInit.GPIO_Pin=GPIO_Pin_9;
    GPIO_Init(GPIOA, &gpioInit);
   
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    gpioInit.GPIO_Pin=GPIO_Pin_10;
    GPIO_Init(GPIOA, &gpioInit);
   
}

void delay(unsigned long uDelay) {
    while(--uDelay);
}
 

coder

Sinh viên đại học
#18
Ví dụ USART interrupt
PHP:
#include "main.h"

GPIO_InitTypeDef gpioInit;
USART_InitTypeDef usartInit;

void init(void);
void delay(unsigned long uDelay);
void NVIC_Config(void);

static uint8_t tmp;

int main() {
    init();
    GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
    while(1) {
        /*
        //cho den khi nhan duoc 1 byte thi thoi
        while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)==RESET);
        tmp=USART_ReceiveData(USART1);
        //xoa co tran
        USART_ClearFlag(USART1, USART_FLAG_RXNE);
        if(tmp=='a') {
            GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
        if(tmp=='b') {
            GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
       
        USART_SendData(USART1, tmp+2);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
        USART_ClearFlag(USART1, USART_FLAG_TC);
        */
    }
}

void init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &gpioInit);
   
    //khoi tao USART1
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    usartInit.USART_BaudRate=9600;
    usartInit.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    usartInit.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    usartInit.USART_Parity=USART_Parity_No;
    usartInit.USART_StopBits=USART_StopBits_1;
    usartInit.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART1, &usartInit);
    USART_Cmd(USART1, ENABLE);
   
    //khoi tao GPIO PA9 PA10
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Mode=GPIO_Mode_AF_PP;
    gpioInit.GPIO_Pin=GPIO_Pin_9;
    GPIO_Init(GPIOA, &gpioInit);
   
    gpioInit.GPIO_Speed=GPIO_Speed_50MHz;
    gpioInit.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    gpioInit.GPIO_Pin=GPIO_Pin_10;
    GPIO_Init(GPIOA, &gpioInit);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    NVIC_Config();
}

void NVIC_Config(void) {
    NVIC_InitTypeDef nvicInit;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
   
    nvicInit.NVIC_IRQChannel = USART1_IRQn;
    nvicInit.NVIC_IRQChannelSubPriority = 0;
    nvicInit.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvicInit);
}

void delay(unsigned long uDelay) {
    while(--uDelay);
}
PHP:
#include "stm32f10x_it.h"
uint8_t tmp;
void USART1_IRQHandler(void) {
    if(USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET) {
        tmp=USART_ReceiveData(USART1);
        //xoa co tran
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        if(tmp=='a') {
            GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
        if(tmp=='b') {
            GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
        }
       
        USART_SendData(USART1, tmp+2);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
        USART_ClearFlag(USART1, USART_FLAG_TC);
    }
}
 

coder

Sinh viên đại học
#20
cảm ơn coder đã chia sẻ tài liệu. mình rất vui khi có người cũng nghịch cái kit này.

nhưng bạn gửi file hex không thì hạn chế mọi người quá. bạn có thể chia sẻ tài liệu mà bạn dùng để viết fimware USB được chứ.
Mình thấy thư viện USB của ST cũng dễ dùng
Mình đang chỉ dừng lại ở chỗ code chạy được chứ chưa hiểu rõ lắm
Khi nào mình hiểu rõ hơn về cách cấu hình mình sẽ post tiếp.
Hiện tại mình dùng thư viện USB bản 4.0
http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/firmware/stsw-stm32081.zip
 

Quảng cáo Google