Has anyone tried AI with PICBASIC


+ Reply to Thread
Results 1 to 40 of 104

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,124

    Default Re: Has anyone tried AI with PICBASIC

    Can we see that code?
    It is very interesting...

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133

    Default Re: Has anyone tried AI with PICBASIC

    Quote Originally Posted by CuriousOne View Post
    Can we see that code?
    It is very interesting...
    Code:
    ' Define the input serial port
    DEFINE HSER_RXSTA 64
    DEFINE HSER_RCSTA 128
    DEFINE HSER_BAUD 9600
    DEFINE HSER_CLROERR 1
    
    ' Define the size of the ring buffer
    DEFINE BUFFER_SIZE 16
    
    ' Define the ring buffer
    DIM buffer(BUFFER_SIZE) AS BYTE
    DIM buffer_start AS BYTE
    DIM buffer_end AS BYTE
    
    ' Initialize the buffer pointers
    buffer_start = 0
    buffer_end = 0
    
    ' Define the interrupt handler for receiving data on the serial port
    ASM
    INT_LIST macro ; IntSource,        Label,  Type, ResetFlag?
        INT_Handler    HSER_RX_INT,  _receive_data,  PBP,  yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM
    
    ' Define the subroutine to handle incoming data on the serial port
    _receive_data:
        ' Read the incoming byte
        buffer(buffer_end) = HSER_RCREG
    
        ' Update the buffer pointer
        buffer_end = (buffer_end + 1) MOD BUFFER_SIZE
    
        ' Return from the interrupt
        RETURN
    
    ' Main program loop
    DO
        ' Check if there is data in the buffer
        IF buffer_end <> buffer_start THEN
            ' Read the next byte from the buffer
            BYTE next_byte = buffer(buffer_start)
    
            ' Do something with the byte here...
    
            ' Update the buffer pointer
            buffer_start = (buffer_start + 1) MOD BUFFER_SIZE
        ENDIF
    
        ' Insert a delay to avoid consuming too much processing power
        PAUSE 10
    LOOP
    This program defines the input serial port using the DEFINE statement, sets the size of the ring buffer, and initializes the buffer pointers. It then defines an interrupt handler for receiving data on the serial port, which reads incoming bytes and stores them in the buffer. Finally, it sets up a main program loop that reads bytes from the buffer and processes them, and inserts a PAUSE statement to avoid consuming too much processing power. You may need to modify this program slightly to fit your specific requirements, but it should provide a good starting point.

    All the above was the chatGPT answer to a Ring buffer request, including the last paragrapgh, explaining what it did! Very interesting although with some errors.

    Ioannis

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133

    Default Re: Has anyone tried AI with PICBASIC

    After teaching it the correct variable syntax here is the revised program. Note that ASM is not used this time:

    Code:
    'PIC18FxxK22 Configuration Bit Settings
    'CONFIG1H
    #CONFIG
      FOSC = INTIO67    'Internal oscillator block, port function on RA6 and RA7
      PLLCFG = ON       '4X PLL Enable (Oscillator multiplied by 4)
      PRICLKEN = ON     'Primary clock is always enabled
      FCMEN = OFF       'Fail-Safe Clock Monitor disabled
      IESO = OFF        'Oscillator Switchover mode disabled
    #ENDCONFIG
    'CONFIG2L
    #CONFIG
      PWRTEN = ON       'Power up timer enabled
      BOREN = SBORDIS   'BOR enabled in hardware (SBOREN is ignored)
      BORV = 2          'Brown-out Reset Voltage bits (2.7V)
      BORPWR = ZPBORMV  'BORMV set to ZPBORMV instead of ZPBORMV42
      WDTEN = OFF       'WDT disabled (control is placed on SWDTEN bit)
      WDTPS = 32768     'Watchdog timer postscaler
    #ENDCONFIG
    'CONFIG2H
    #CONFIG
      CCP2MX = PORTC1   'CCP2 input/output is multiplexed with RC1
      PBADEN = OFF      'PORTB<5:0> pins are configured as digital I/O on reset
      LPT1OSC = OFF
    #ENDCONFIG
    
    ' Declare variables
    RingBuffer var Byte[16]    ' Ring buffer to store received data
    RxBufIndex var Byte        ' Index to the next empty location in the ring buffer
    RxCtr var Byte             ' Counter to keep track of the number of received bytes
    RxByte var Byte            ' Variable to store the received byte
    
    ' Initialize variables
    RxBufIndex = 0               ' Start from the beginning of the ring buffer
    RxCtr = 0                    ' No bytes received yet
    
    ' Main program loop
    Do
        ' Check if there is any data available in the UART receive buffer
        If UART1_Data_Ready() Then
            ' Read the received byte from the UART receive buffer
            RxByte = UART1_Read()
            
            ' Store the received byte in the ring buffer
            RingBuffer[RxBufIndex] = RxByte
            
            ' Update the index to the next empty location in the ring buffer
            RxBufIndex = (RxBufIndex + 1) Mod 16
            
            ' Increment the counter to keep track of the number of received bytes
            RxCtr = RxCtr + 1
            
            ' Check if all 16 bytes have been received
            If RxCtr = 16 Then
                ' Do something with the received data
                
                ' Reset the counter and start over
                RxCtr = 0
            End If
        End If
    Loop
    Ioannis

Similar Threads

  1. conversion from picbasic to picbasic pro
    By winjohan in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st November 2011, 18:00
  2. Proton PICBASIC vs MeLabs PICBASIC
    By Fredrick in forum General
    Replies: 22
    Last Post: - 11th January 2008, 21:51
  3. PICBasic Pro vs Proton PICBasic
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 3rd November 2006, 16:11
  4. picbasic 2.46
    By jojokatada in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th April 2005, 03:34
  5. PicBasic Pro & PicBasic syntax different
    By Billyc in forum General
    Replies: 5
    Last Post: - 16th April 2004, 21:19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts