PIC16LF1455 USB 的调试

       芯片涨价,又回到microchip 公司的pic16单片机。这次使用PIC16LF1455 .一个14Pin 的单片机居然还带一个USB 。真是个好东西。

    测试过程在Microchip 公司的MPLAB X IDE v5.5下进行。

使用MCC 配置时钟

ACTEN Enable 

配置USB 

代码 

/*
 * File:   main.c
 * Author: asus
 *
 * Created on 2021?6?23?, ??9:55
 */


#include <xc.h>
#include "mcc_generated_files/mcc.h"
#include <stdint.h>

#include "mcc_generated_files/usb/usb.h"

static uint8_t readBuffer[64];
static uint8_t writeBuffer[64];
void MCC_USB_CDC_DemoTasks(void)
{
    if( USBGetDeviceState() < CONFIGURED_STATE )
    {  
     //   __delay_ms(500);
    //    IO_RC2_Toggle();
        return;
    }

    if( USBIsDeviceSuspended()== true )
    {
        return;
    }

    if( USBUSARTIsTxTrfReady() == true)
    {
        uint8_t i;
        uint8_t numBytesRead;

        numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));

        for(i=0; i<numBytesRead; i++)
        {
            switch(readBuffer[i])
            {
                /* echo line feeds and returns without modification. */
                case 0x0A:
                case 0x0D:
                    writeBuffer[i] = readBuffer[i];
                    break;

                /* all other characters get +1 (e.g. 'a' -> 'b') */
                default:
                    writeBuffer[i] = readBuffer[i] + 1;
                    break;
            }
        }

        if(numBytesRead > 0)
        {
            putUSBUSART(writeBuffer,numBytesRead);
        }
    }

    CDCTxService();
}
void main(void) {
     SYSTEM_Initialize();
      INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
  //   IO_RC5_SetHigh();
     while(1){
     //    __delay_ms(1);
         MCC_USB_CDC_DemoTasks();
      //    IO_RC2_Toggle();
     }
    return;
}

读出CPU内部的温度

老外正会玩,搞了个最小的USB 温度计,PIC16LF1455 内部有一个温度传感器。

main 代码

void main(void) {
     SYSTEM_Initialize();
      INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
  //   IO_RC5_SetHigh();
    softTimer=0;
     while(1){
         __delay_ms(10);
         softTimer++;
         if (softTimer==50){
             softTimer=0;
             IO_RC2_Toggle();
             adc_result_t tempADC=ADC_GetConversion(channel_Temp);
            float temperature = 0.925679 * tempADC - 487.727;
            int len=sprintf(writeBuffer,"T=%.2f\n",temperature);
             putUSBUSART(writeBuffer,len);
         }
     //  MCC_USB_CDC_DemoTasks();
           CDCTxService();
      //    IO_RC2_Toggle();
     }
    return;
}

猜你喜欢

转载自blog.csdn.net/yaojiawan/article/details/118159557