• suksun
  • ranking : สมาชิกทั่วไป
  • email : suksun_nungam@yahoo.com
  • วันที่สร้าง : 2007-05-17
  • จำนวนเรื่อง : 5
  • จำนวนผู้ชม : 1679
  • จำนวนผู้โหวต : 7
  • ส่ง msg :
dsPic
การเขียนโปรแกรมเพื่อใช้สำหรับ dsPic
Permalink : http://www.oknation.net/blog/suksun
วันจันทร์ ที่ 21 พฤษภาคม 2550
การติดพอร์ทอนุกรม(UART) dsPIC
Posted by suksun , ผู้อ่าน : 237 , 14:15:12 น.  
พิมพ์หน้านี้


;This program tests UART
;=============================================================================;
;
; Use Timer 1 to flash LED
;
;==============================================================================
.equ __30F2010, 1
.include "p30f2010.inc"
.include "serial_comm.inc"
;------------------------------------------------------------------------------
;Global Declarations
.global __reset              ;The label for the first line of code
.global __OscillatorFail     ;Declare Oscillator Fail trap routine label
.global __AddressError       ;Declare Address Error trap routine label
.global __StackError         ;Declare Stack Error trap routine label
.global __MathError          ;Declare Math Error trap routine label
;------------------------------------------------------------------------------
;Configuration bits
config __FOSC, CSW_FSCM_ON & XT_PLL16
config __FWDT, WDT_OFF
config __FBORPOR, PBOR_OFF & BORV_27 & PWRT_16 & MCLR_EN
config __FGS, CODE_PROT_OFF


;------------------------------------------------------------------------------
;Program Specific Constants (literals used in code)
.equ FCY, #7372800           ;Instruction cycle rate (Osc x PLL / 4)      

;==============================================================================
;Start of code
.text ;Start of Code section
;------------------------------------------------------------------------------
;Initialize stack pointer and limit register
__reset:   mov #__SP_init, W15 ;Initialize the Stack Pointer register
               mov #__SPLIM_init, W0        ;Get address at the end of stack space
               mov W0, SPLIM                ;Load the Stack Pointer Limit register
               nop                          ;Add NOP to follow SPLIM initialization
;------------------------------------------------------------------------------
 
;Initialize LED output on PORTB bit 0
            mov #0xff00,W0               ;suk config PORTB<15:8> as inputs
            mov W0,TRISB                 ;suk config PORTB<7:0> as outputs
            mov #0xffff,W0
            mov W0,TRISE                 ;suk Confic PORTE as inputs
   ;mov #0x0000,W0
            ;mov W0,TRISC    
;------------------------------------------------------------------------------
;Initialize Timer1 for 1/1 second period
         /*   clr T1CON                    ;Turn off Timer1 by clearing control register
   clr TMR1                     ;Start Timer1 at zero
   mov #FCY/256/1,W0            ;Get period register value for 1/5 second
   mov W0, PR1                  ;Load Timer1 period register
   mov #0x8030,W0               ;Get Timer1 settings (1:256 prescaler)
   mov W0, T1CON                ;Load Timer1 settings into control register
            */
;------------------------------------------------------------------------------
;Initialize UART
       CALL Init_UART ;

       CALL _NewPage 
;Loop while waiting for a Timer1 match and toggle LED1 when it happens
            MOV #0x0000,W1
MainLoop:  
           btss U1STA,#URXDA
            bra MainLoop
            MOV U1RXREG,W7;
       ;mov W2,U1TXREG
            
            ;CALL _NewLine
    ;CALL SEND_
  /* btss IFS0, #T1IF             ;Check if Timer1 interrupt flag is set
           bra MainLoop                 ;Loop back until set
           bclr IFS0, #T1IF             ;Clear Timer1 interrupt flag
           ;btg LATD, #0  */               ;Toggle LED
           
   ;Call Delay   
            MOV W1,W4
   COM W4,W4
   MOV W4,LATB
   
   ;btg LATB, #0                 ;SUk
   ;btg LATC, #13 
            CALL Display ;

         INC W1,W1 ;

            bra MainLoop                 ;Loop back

;==============================================================================
/*   clr U1STA
   mov #0x8000,W0 ; enable UART module
   mov W0,U1MODE
   mov #BR,W0 ; set baudrate using formula value
   mov W0, U1BRG ; /
   bset U1STA,#UTXEN ; initiate transmission
Again:
   rcall Delay500mSec ; delay for 500 mS
   mov #psvpage(hello),w0
   mov w0, PSVPAG
   bset.b CORCONL,#PSV
   mov #psvoffset(hello),w0
TxSend:
   mov.b [w0++], w1 ; get char in string
   cp w1,#0 ; if Null
   bra Z,Again ; then re-initialize
BufferTest:
   btsc U1STA,#UTXBF ; see if buffer full
   bra BufferTest ; wait till empty
   mov w1,U1TXREG ; load value in TX buffer
   bra TxSend ; repeat for next char.


*/

;==============================================================================
;Error traps
;------------------------------------------------------------------------------
;Oscillator Fail Error trap routine
.text                        ;Start of Code section
__OscillatorFail:
   bclr LATD, #0                ;Turn LED on
   bra __OscillatorFail         ;Loop forever when oscillator failure occurs
;------------------------------------------------------------------------------
;Address Error trap routine

__AddressError:
   bclr LATD, #0                ;Turn LED on
   bra __AddressError           ;Loop forever when address error occurs
;------------------------------------------------------------------------------
;Stack Error trap routine
__StackError:
   bclr LATD, #0                ;Turn LED on
   bra __StackError             ;Loop forever when stack error occurs
;------------------------------------------------------------------------------
;Math (Arithmetic) Error trap routine
__MathError:
   bclr LATD, #0                ;Turn LED on
   bra __MathError              ;Loop forever when math error occurs
;==============================================================================
Delay:
            mov #0x001f,w4
Again2:  mov #0xffff,W3        ;
Again1:     DEC W3,W3             ;
   BRA NZ, Again1
            DEC W4,W4
            BRA NZ, Again2
            RETURN  

;=============================================================================

.equ baudrate,#47; #0x00bf; #191       ;suk Set baudrate

.text
 Init_UART:           ;Initialize UART
   CLR U1STA
   CLR U1MODE
   MOV #0x8000,W0 ; Enable UART for 8-bit data,
       ; no parity, 1 STOP bit,
       ; no wakeup
   MOV W0,U1MODE
   BSET U1MODE,#ALTIO
   MOV #baudrate,W0 ; Set Baudrate
   MOV W0,U1BRG
   ;BSET IPC2,#U1TXIP2 ; Set UART TX interrupt priority
   ;BCLR IPC2,#U1TXIP1 ;
   ;BCLR IPC2,#U1TXIP0 ;
   ;BSET IPC2,#U1RXIP2 ; Set UART RX interrupt priority
   ;BCLR IPC2,#U1RXIP1 ;
   ;BCLR IPC2,#U1RXIP0 ;
  
   BSET U1STA,#UTXEN ; Enable transmit
   ;BSET IEC0,#U1TXIE ; Enable transmit interrupts
   ;BSET IEC0,#U1RXIE ; Enable receive interrupts
            RETURN          

Display:
            CALL _Home
            CALL _NewLine
            CALL _Space
   
   CALL BufferTest ; wait till empty
   mov.b #'S',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'u',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'k',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'s',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'u',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
            mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
      MOV W1,W2;
            CALL SEND_ ;
 
           
            CALL _Tab 
      CALL BufferTest ; wait till empty
         mov.b #'U',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'1',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'T',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'X',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'R',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;
     
   CALL _NewLine ;
            CALL _Return ;
            CALL _Space;

   CALL BufferTest ; wait till empty
         mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'W',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'1',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;

    CALL _Tab 
      CALL BufferTest ; wait till empty
         mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'W',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'2',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;
    CALL _NewLine ;
            CALL _Return ;
            CALL _Space;

   CALL BufferTest ; wait till empty
         mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'W',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'3',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;

    CALL _Tab 
      CALL BufferTest ; wait till empty
         mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'W',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'4',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;  
        
   CALL _NewLine ;
            CALL _Return ;
            CALL _Space;

   CALL BufferTest ; wait till empty
         mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'W',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'5',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;

    CALL _Tab 
      CALL BufferTest ; wait till empty
         mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #'W',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'6',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG
   CALL BufferTest ; wait till empty
   mov.b #'=',W0
   mov W0,U1TXREG
         CALL BufferTest ; wait till empty
   mov.b #' ',W0
   mov W0,U1TXREG  
   MOV W1,W2;
            CALL SEND_ ;
           
           
           
   RETURN ;


SEND_:     DO #3,END2 
        
   DO #3,END1
   RLC W2,W2
   BRA C, CARRY
NO_C:

   CALL BufferTest ; wait till empty
   mov.b #'0',W0  ;SNED '0'
   mov W0,U1TXREG
            GOTO END1
CARRY:

            CALL BufferTest
   mov.b #'1',W0  ;SEND '1'
   mov W0,U1TXREG
            NOP 
END1:  NOP
   CALL BufferTest; wait till empty
   mov.b #' ',W0  ;SEND ''
   mov W0,U1TXREG
END2:       NOP 
            RETURN

BufferTest:
      btsc U1STA,#UTXBF ; see if buffer full
      bra BufferTest ; wait till empty
            RETURN

_NewPage:   
   CALL BufferTest ; wait till empty
   mov.b #0x0C,W0  ;New page =0x0C
   mov W0,U1TXREG
      RETURN

_NewLine:   
   CALL BufferTest ; wait till empty
      mov.b #0x0A,W0 ; New Line =0x0A
            mov W0,U1TXREG ;
            RETURN
_Return:     
   CALL BufferTest ; wait till empty
   mov.b #0x0D,W0 ;  Return =0x0D
            mov W0,U1TXREG ;
            RETURN
_Space:   
   CALL BufferTest ; wait till empty   
   mov.b #0x20,W0  ; Space=0x20   
   mov W0,U1TXREG
            RETURN
_Tab:   
   CALL BufferTest ; wait till empty   
   mov.b #0x09,W0  ; Horizontal Tab =0x09   
   mov W0,U1TXREG ;
            RETURN
_Home:   
           CALL BufferTest ; wait till empty
           mov.b #0x1B,W0  ;    
     mov W0,U1TXREG ;
           CALL BufferTest ; wait till empty
           mov.b #0x5B,W0  ;    
     mov W0,U1TXREG ;
           CALL BufferTest ; wait till empty
           mov.b #0x48,W0  ;    
     mov W0,U1TXREG ;
           RETURN    

.end                         ;End of code in this file


อ่านความคิดเห็น

ความคิดเห็นที่ 2
Freedomheart วันที่ : 21/05/2007 เวลา : 17.25 น.
http://www.oknation.net/blog/Freedomheart


.มาอ่าน...มาดู...มาศึกษา...............................
...มาเป็นกำลังใจ........................................
...รู้ว่ากำลัง...Post สิ่งดีๆ...ให้เพื่อน..ชาว OKNation/Blog...

...Post เรื่องต่อๆไปนะ.................................
...นับถือ...ในความเสียสละ..............................
ความคิดเห็นที่ 1
suksun วันที่ : 21/05/2007 เวลา : 15.29 น.
http://www.oknation.net/blog/suksun

ค่อยๆ อ่าน จะเข้าใจ อย่า งงๆๆๆๆ
แสดงความคิดเห็น

  เข้าสู่ระบบ   |   สมัครสมาชิก
ชื่อ:  
อีเมล์:  
เว็บไซต์:  
ความคิดเห็น:  
   

ถึง บล็อกเกอร์ ทุกท่าน โปรดอ่าน
   ด้วยทาง บริษัท จีเอ็มเอ็ม แกรมมี่ จำกัด (มหาชน) ได้ติดต่อขอความร่วมมือ มายังเว็บไซต์และเว็บบล็อกต่าง ๆ รวมไปถึงเว็บบล็อก OKnation ห้ามให้มีการเผยแพร่ผลงานอันมีลิขสิทธิ์ ของบริษัท จีเอ็มเอ็ม แกรมมี่ฯ บนเว็บ blog โดยกำหนดขอบเขตของสิ่งที่ห้ามทำ และสามารถทำได้ ดังนี้
ห้ามทำ
- การใส่ผลงานเพลงต้นฉบับให้ฟัง ทั้งแบบควบคุมเพลงได้ หรือซ่อนเป็นพื้นหลัง และทั้งที่อยู่ใน server ของคุณเอง หรือ copy code คนอื่นมาใช้
- การเผยแพร่ file ให้ download ทั้งที่อยู่ใน server ของคุณเอง หรือฝากไว้ server คนอื่น
สามารถทำได้
- เผยแพร่เนื้อเพลง ต้องระบุชื่อเพลงและชื่อผู้ร้องให้ชัดเจน
- การใส่เพลงที่ร้องไว้เอง ต้องระบุชื่อผู้ร้องต้นฉบับให้ชัดเจน
จึงเรียนมาเพื่อโปรดปฎิบัติตาม มิเช่นนั้นทางบริษัท จีเอ็มเอ็ม แกรมมี่ฯ จะให้ฝ่ายดูแลลิขสิทธิ์ ดำเนินการเอาผิดกับท่านตามกฎหมายละเมิดลิขสิทธิ์
OKNATION



กฎกติกาการเขียนเรื่องและแสดงความคิดเห็น
1 การเขียน หรือแสดงความคิดเห็นใด ๆ ต้องไม่หมิ่นเหม่ หรือกระทบต่อสถาบันชาติ ศาสนา และพระมหากษัตริย์ หรือกระทบต่อความมั่นคงของชาติ
2. ไม่ใช้ถ้อยคำหยาบคาย ดูหมิ่น ส่อเสียด ให้ร้ายผู้อื่นในทางเสียหาย หรือสร้างความแตกแยกในสังคม กับทั้งไม่มีภาพ วิดีโอคลิป หรือถ้อยคำลามก อนาจาร
3. ความขัดแย้งส่วนตัวที่เกิดจากการเขียนเรื่อง แสดงความคิดเห็น หรือในกล่องรับส่งข้อความ (หลังไมค์) ต้องไม่นำมาโพสหรือขยายความต่อในบล็อก และการโพสเรื่องส่วนตัว และการแสดงความคิดเห็น ต้องใช้ภาษาที่สุภาพเท่านั้น
4. พิจารณาเนื้อหาที่จะโพสก่อนเผยแพร่ให้รอบคอบ ว่าจะไม่เป็นการละเมิดกฎหมายใดใด และปิดคอมเมนต์หากจำเป็นโดยเฉพาะเรื่องที่มีเนื้อหาพาดพิงสถาบัน
5.การนำเรื่อง ภาพ หรือคลิปวิดีโอ ที่มิใช่ของตนเองมาลงในบล็อก ควรอ้างอิงแหล่งที่มา และ หลีกเลี่ยงการเผยแพร่สิ่งที่ละเมิดลิขสิทธิ์ ไม่ว่าจะเป็นรูปแบบหรือวิธีการใดก็ตาม 6. เนื้อหาและความคิดเห็นในบล็อก ไม่เกี่ยวข้องกับทีมงานผู้ดำเนินการจัดทำเว็บไซต์ โดยถือเป็นความรับผิดชอบทางกฎหมายเป็นการส่วนตัวของสมาชิก
คลิ้กอ่านเงื่อนไขทั้งหมดที่นี่"
OKnation ขอสงวนสิทธิ์ในการปิดบล็อก ลบเนื้อหาและความคิดเห็น ที่ขัดต่อความดังกล่าวข้างต้น โดยไม่ต้องชี้แจงเหตุผลใดๆ ต่อเจ้าของบล็อกและเจ้าของความคิดเห็นนั้นๆ
   

กลับไปหน้าที่แล้ว กลับด้านบน

<< พฤษภาคม 2007 >>
อา พฤ
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31